rapideditor / country-coder Goto Github PK
View Code? Open in Web Editor NEWπβ‘οΈ π©π° Convert longitude-latitude pairs to ISO 3166-1 codes quickly and locally
Home Page: https://ideditor.codes
License: ISC License
πβ‘οΈ π©π° Convert longitude-latitude pairs to ISO 3166-1 codes quickly and locally
Home Page: https://ideditor.codes
License: ISC License
It's the end of Russia hanging out by Alaska: https://en.wikipedia.org/wiki/Chukchi_Peninsula
This is not a high priority issue, but maybe a nice-to-have.
CountryCoder and LocationConflation are integrated into Name Suggestion Index now which is pretty cool..
Check out the entries on https://nsi.guide to see what I mean:
A thing I am running into is:
Some countries like US, GB, FR include a bunch of sub territories. Technically the US includes Marshall Islands and GB includes Bermuda. FR is nice because we can use the code FX to mean "Metropolitan France". But for some situations, it would be really nice to have dedicated code strings for:
I'm not familiar with what all countries contain territories, and not sure to what extent Country Coder already includes some of these.. just brainstorming that it would be nice to say 'conus' or 'scotland' to return just those parts.
I'd like to be able to also get regions, which would sit at a level higher than countries.
Each country could belong to a single region, and there could be a lookup table or something.
There are many ways to slice up the world, but I'm thinking these 12 (+ Antarctica) would give us a bunch of flexibility:
This is kind of a well discussed topic, see
https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea
But also raised on #59 as something that was causing an issue in another library. We should just change our code to not do that.
Originally posted by @bhousel in #59 (comment)
It would be great to get a feature for the world that just covers the whole world. I know it seems silly but we do have worldwide resources in the osm-community-index project, and I would use this code for them.
re: https://en.wikipedia.org/wiki/UN_M49
> CC.feature('001')
null
At geo:47.56982,8.35154 there is a missing triangle, which should probably be added to the ch
region.
Hi,
Any chance of a new release so we can have access to all the new sub-places:
v3.2.0...master
To be honest, I'm not sure where would be best to ask about this, either here, location-conflation or the Name Suggestion Index, so apologies if this isn't relevant here.
Basically, over on the Name Suggestion Index, there is a /features/ folder that I've expanded recently, and it contains .geojson files for mapping certain areas that a brand should be limited to, and this is powered by location-conflation and seems to work well.
However, I've noticed that the osm-community-index has the same concept, with a /features/ folder linking areas where an OSM community group is based.
Within both projects are similar region files, and I can see both projects expanding with similar data. One example being the West Midlands in England (NSI vs OCI) which is basically the same region.
I imagine other projects exist (or will exist) with the same goal of having sub-regions where the .geojson files are similar.
I gather that this project (country-coder) doesn't intend to expand into sub-regions, so as to prevent it becoming too large, and I can understand that.
However, is there any scope for hosting sub-region .geojson files within the repository (or location-conflation) so there is a central place for users to reference the region files, without having to recreate them for their own project?
Re: #4
https://en.wikipedia.org/wiki/Country_code_top-level_domain
Mostly these are just the ISO alpha-2 codes, so we only need to explicitly code any exceptions.
Heya,
Would you be open to accepting a PR to add 'Official Languages' per-region?
I hunted around for an official source & didn't find many decent ISO 3166 <> 639 mapping files.
This file from geonames contains a bunch of extra fields we could adopt, including the languages (which are correct from my spot checking):
http://download.geonames.org/export/dump/countryInfo.txt
Another option would be to use a locale
dataset from a linux distro, I feel like they would be fairly complete and well maintained.
Please let me know if that's something you'd accept.
For consistency, we store the numeric codes as string types. However, users should be able to pass in number types and get the same results.
Package.json exports does not specify types, so declaration are not found when exports supported (moduleResolution: bundler in tsconfig)
It should be specified in package.json as:
"type": "module",
"source": "./src/country-coder.ts",
"types": "./dist/country-coder.d.ts",
"main": "./dist/country-coder.cjs",
"module": "./dist/country-coder.mjs",
"exports": {
+ "types": "./dist/country-coder.d.ts",
"import": "./dist/country-coder.mjs",
"require": "./dist/country-coder.cjs"
},
TS7016: Could not find a declaration file for module '@rapideditor/country-coder'. 'my-project/node_modules/@rapideditor/country-coder/dist/country-coder.mjs' implicitly has an 'any' type. Β Β There are types at 'my-project/node_modules/@rapideditor/country-coder/dist/country-coder.d.ts', but this result could not be resolved when respecting package.json "exports". The '@rapideditor/country-coder' library may need to update its package.json or typings.
European Union membership for the UK seems to be disappearing soon...
We already have the European Union. It'd be nice to add the United Nations too. Almost all territories comprise UN member states with some exceptions:
First of all thanks a lot for this awesome lib. I was using codegrid but it's not maintained anymore and yours offers so much more features.
I'm using it in a Vue + Vite project, iso1A2Code() works like a charm and returns the country code, but everytime i'm passing {level: 'territory'} i'm getting this error, same for iso1A2Codes(), features() and more.
I guess it's probably be some esm issues,
I changed the vite config to optimizeDeps: { exclude: [ '@ideditor/country-coder' ] }
but now i'm getting this: Uncaught SyntaxError: The requested module '/node_modules/which-polygon/index.js?v=3baa6ba3' does not provide an export named 'default'
.
To me it seems to come from which-polygon, i took a look at the source code and they seem to use commonJS. I've read vite may have similar issues because of librairies not fully ESM compatibles.
You can close this issue it's probably me and my lack of knowledge, but if anyone has a solution to this let me know !
Thanks
Re: #4
We can replace this file: https://github.com/openstreetmap/iD/blob/v2.16.0/data/mph.json
List of regions: https://en.wikipedia.org/wiki/Miles_per_hour#Road_traffic
See also: openstreetmap/iD#6626
File size is a key factor of this project. We can remove white-space characters to make the data file smaller.
Currently when taking dependency on country-coder npm package a bundle will get heavier by ~600kb.
The main reason is due to borders file which takes 595kb and is directly imported in source code.
e.g. running build analysis in iD would tell that iD bundle is 3MB while country-coder takes 600kb (~15%), however it seems that geojson is not the best candidate for web-standard compressions (e.g. br) which is why this piece takes ~35% of the compressed bundle.
Making such bundle overhead impacts fcp (especially with users that have low speed connection) which can be avoided in case when borders.json is not necessary for startup/fcp.
Create lazy/dynamic load of the borders file inside the country-coder.
Potential implementation is to have init()/ensureLoaded() method to request+load the heavy file and return the promise which then gets resolved. Unfortunately, that would result in shell pattern (unpopulated object which enables default 'empty' functionalities which become functional after init is done), however in this case it would be worth it.
I'm getting this error downstream in Name-Suggestion-Index after upgrading location-conflation/country-coder:
The line that triggers the error is:
const iso1A2Code = require('@ideditor/country-coder').iso1A2Code;
I did not expect that by adding "exports" to the package.json
that a CJS/require environment would try to use it and pull in a modern ES6 module.
Anyway here is some stuff for me to read later: https://nodejs.org/api/packages.html#packages_package_entry_points
It might be best to avoid the exports
field for now and just keep using the main
, module
, browser
fields.
(This might even be fixed already since the switchover to esbuild - I dont know)
Looking into this issue: osmlab/osm-community-index#334 (comment)
I'm using location-conflation to process the features with turf. In this case we're unioning all of the gb
features together, but many of them get left out. For example, Isle of Man shows up as a hole:
My guess is that both the GeoJSON spec and Turf are picky about polygon winding, and that some of the features in Country Coder are not wound consistently.
- A linear ring MUST follow the right-hand rule with respect to the area it bounds, i.e., exterior rings are counterclockwise, and holes are clockwise.
We can probably just run everything through https://github.com/mapbox/geojson-rewind to make sure they are following the right hand rule.
that's it that's the issue
hi !
This package is awesome so, i forked it and wanted to put 0 dependencies.
I add typescript on this js package (2) [...].
well... I don't want make pull request because want to make some custom stuff with borders (babylon 5 map)
export interface GeoBorderProps {
wikidata?: string;
nameEn?: string;
aliases?: string[];
country?: string;
groups?: string[];
driveSide?: "right" | "left";
roadSpeedUnit?: "mph" | "km/h";
roadHeightUnit?: "ft" | "m";
callingCodes?: string[];
iso1A2?: string,
iso1A3?: string
level?: GeoLevel,
m49?: string,
iso1N3?: string,
isoStatus?: "usrAssn" | "excRes" | "official",
ccTLD?: string,
emojiFlag?: string,
members?: string[],
maxLevel?: string | undefined;
withProp?: string | undefined;
id?: string,
}
export type GeoBorderKeys= keyof GeoBorderProps
I hope it helps
Since Bir Tawil isn't part of any country, country-coder doesn't know that it's part of Africa. We should add a region just to fill in this hole.
The CountryCoder
class doesn't actually manage any state except the index caches. Since these never change and could be shared by various callers of the package, it'd be better to move the caches to the module level and remove the need for a class at all.
Regrettably, this is a big breaking change.
Oops, wrong tab/repo! see osmlab/name-suggestion-index#4815 instead π
The UK boundary is showing as including the British Overseas Territories and Crown dependencies. I believe this should be excluded. The have their own ISO codes.
https://ideditor.github.io/location-conflation/?locationSet=%7B%20include%3A%20%5B%22GB%22%5D%7D
I think the region that it should show is:
https://ideditor.github.io/location-conflation/?locationSet=%7B%20include%3A%20%5B%22GB-ENG%22%2C%20%22GB-NIR%22%2C%20%22GB-SCT%22%2C%20%22GB-WLS%22%5D%7D
Details on the British Overseas Territories and Crown dependencies can be found here:
https://en.wikipedia.org/wiki/United_Kingdom#Dependencies
Currently, any request with coordinates pointing to Northen Cyprus, returns no country code.
Its entry in borders.json does indeed have no codes specified:
{
"type":"Feature",
"properties":{
"wikidata":"Q23681",
"nameEn":"Northern Cyprus",
"groups":[
"Q644636",
"145",
"142"
],
"driveSide":"left",
"callingCodes":[
"90 392"
]
},
...
},
However, even though the Turkish Republic of Northern Cypr is politically separated from Cyprus (Republic of Cyprus), it should share the same country code CY
. Here's why:
I've started using country-coder
in my OpenStreetMap Community Viewer.
I'm trying to "guess" the region/continent a country is from using country-coder
groups.
It works pretty well ... except for France (and I guess other countries in a similar "situation") !
Groups for France are ["UN", "001"]
(see https://ideditor.codes/?code=Q142).
There is definitely EU
missing !
But I would also expect 150
(Europe) ; I guess this happens because France has territories outside Europe (same happens to Spain).
EU
missing ? France is one of the founding member of European Union (https://en.wikipedia.org/wiki/Member_state_of_the_European_Union)Thanks for this awesome library :)
Let's brainstorm here on what other data we want to include for each country. Would be great to remove some of these datasets from iD.
Hi!
Great library!
I was wondering whether you were planning to add Canadian provinces and territories as well as USA states as territories (or other type of features) in the data set?
Names are convenient for debugging and for convenience. For instance it's easy to read and write isIn([23, 78.2], 'Asia')
instead of looking up a region code.
Some countries, which have a ISO 3166-1 entry return the Sovereign state's country code.
Example:
"lng": -65.0481,
"lat": 32.177
is a location in Bermuda, which has a country code: https://www.iso.org/obp/ui/#iso:code:3166:BM . However, GB
gets returned (I suppose because in its entry there is a "country":"GB"
property).
react-native version: 0.64.2
import * as countryCoder from '@ideditor/country-coder'; // ES6 import all
import { iso1A2Code } from '@ideditor/country-coder';
Neither works, it will just show as undefined function.
[TypeError: undefined is not a function]
Other exports don't work as well, I suspect something to do with your exports configuration?
import { feature, iso1A2Code, emojiFlag } from '@ideditor/country-coder';
I reverted back my upgrade, the latest version of ideditor/country-coder that works in a react-native (non-expo) environment is v4.1.0
which-polygon has a bbox query, which would be amazing if you could support. The use case is: I'm looking at a map and I want to find out all countries that are on the screen given a min/max lat,lng pairs.
Happy to attempt to add this myself, I just thought I'd make the suggestion as it will probably takes the maintainers much less time to implement.
Re: #4
We can replace this file: https://github.com/openstreetmap/iD/blob/v2.16.0/data/drive-left.json
List of regions: https://en.wikipedia.org/wiki/Left-_and_right-hand_traffic#Worldwide_distribution_by_country
@bhousel Thank you very much for the help. Just one small thing left before package is fully ready for RN again.
Yes browser export was causing issues, and I get the actual root issue now aside from browser export.
It's the file extension pattern matching.
This is v4.1.0 package.json, it conforms to pattern
"main": "./dist/country-coder.js",
"module": "./dist/country-coder.module.js",
it conforms to this pattern that React-Native wants, notice the .js extension (.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.svg|.native.svg|.svg)
, without this Xcode will not build.
However in v5.0.3, it becomes
"main": "./dist/country-coder.cjs",
"module": "./dist/country-coder.mjs",
If I just added a .js to both of these files (RN uses module but both pattern matching was needed)
"main": "./dist/country-coder.cjs.js",
"module": "./dist/country-coder.mjs.js",
This works perfectly. All features are now back online. It's silly, but the pattern matching is strict.
This works for most flags, but Metropolitan France uses the code fx
which doesn't have a flag. Maybe it should just hardcode fr
here?
I'm not sure if there are any other countries like this. Metropolitan France is kind of weird.
The coder currently expects all codes to be uppercase (e.g. CN
). We should accept lowercase or mixed-case codes too (e.g. cn
, Cn
).
Of the "TPKM" areas the TW polygon currently includes Taiwan, Penghu and the main island of Kinmen County, but there are a few islands missing. In keeping with the objectives of this library the motivation here is to power consistent locale information for mapping, so this doesn't include some unpopulated islands; the civilian population is 10,000+ of these in total and there is significant detail in OSM:
Wuqiu relation on OSM
Lienchiang (Matsu) relation on OSM
Screenshot of proposed polygon change with OSM Carto as a reference, using a minimal number of vertices:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.