Small fixes and usability improvements
This commit is contained in:
parent
e873a7cf70
commit
2752729092
9 changed files with 39 additions and 10 deletions
|
@ -46,10 +46,10 @@ The requested tiles all have the extension '.json'. THey however break down into
|
|||
|
||||
A 'leaf'-tile is a tile with which the actual country can be determined without downloading more tiles.
|
||||
|
||||
If the result is uniform accross the tile (most commonly: the tile is completely within a single country), then the tile
|
||||
If the result is uniform across the tile (most commonly: the tile is completely within a single country), then the tile
|
||||
will consist of `["country_code"]`
|
||||
|
||||
If a border goes trhough the tile, the tile will contain the actual geometries and the leaftile is a standard geojson
|
||||
If a border goes through the tile, the tile will contain the actual geometries and the leaftile is a standard geojson
|
||||
file.
|
||||
|
||||
The client library will enumerate _all_ the polygons and determine in which polygons the requested points lie. Multiple
|
||||
|
|
|
@ -27,7 +27,6 @@ export default class DownloadAllBoundaries {
|
|||
// Lebanon
|
||||
|
||||
const firstCountry = queue.pop();
|
||||
console.log("NExt target:", firstCountry)
|
||||
new DownloadBoundary(firstCountry).PerformOrFromCache(() => firstCountry,
|
||||
geojson => {
|
||||
if (geojson !== null) {
|
||||
|
|
|
@ -26,6 +26,7 @@ export default class DownloadCountryCodes extends Step {
|
|||
public Step(_, done: (countryCodes: string) => void) {
|
||||
const query = "https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20%3FitemLabel%20%3Flabel%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cen%22.%20%7D%0A%20%20%3Fitem%20wdt%3AP297%20%3Flabel.%0A%7D%0ALIMIT%20500"
|
||||
Utils.Download(query, (data => {
|
||||
console.log("Download country codes from wikidata")
|
||||
const lines = data.split("\n");
|
||||
const codes = []
|
||||
const countries = []
|
||||
|
|
|
@ -11,6 +11,10 @@ import BuildMergedTiles from "./BuildMergedTiles";
|
|||
import SimplifyTile from "./SimplifyTiles";
|
||||
import * as fs from "fs";
|
||||
|
||||
if(!fs.existsSync("../data")){
|
||||
fs.mkdirSync("../data")
|
||||
}
|
||||
|
||||
new DownloadCountryCodes().PerformOrFromCache(() => "", (countries => {
|
||||
|
||||
const countryCodes = {};
|
||||
|
@ -54,7 +58,7 @@ new DownloadCountryCodes().PerformOrFromCache(() => "", (countries => {
|
|||
if (state === TileState.EXISTS) {
|
||||
new SimplifyTile(tileXYZ).PerformOrFromCache(() => {
|
||||
// We need to read the tile from disk
|
||||
return fs.readFileSync(mergedTiles.GetPath(tileXYZ), {encoding: "utf8"})
|
||||
return fs.readFileSync(mergedTiles.GetPath(tileXYZ,".json"), {encoding: "utf8"})
|
||||
}, (result) => {
|
||||
const key = `${tileXYZ.z}.${tileXYZ.x}.${tileXYZ.y}`;
|
||||
|
||||
|
@ -119,7 +123,7 @@ new DownloadCountryCodes().PerformOrFromCache(() => "", (countries => {
|
|||
|
||||
}
|
||||
}
|
||||
console.log("All Done!")
|
||||
console.log("All Done! The necessary tiles are in data/Simplified")
|
||||
})
|
||||
|
||||
})
|
||||
|
|
15
package-lock.json
generated
15
package-lock.json
generated
|
@ -1,16 +1,17 @@
|
|||
{
|
||||
"name": "latlon2country",
|
||||
"version": "1.2.3",
|
||||
"version": "1.2.6",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "latlon2country",
|
||||
"version": "1.2.3",
|
||||
"version": "1.2.6",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"@turf/boolean-point-in-polygon": "^6.0.1",
|
||||
"@turf/turf": "^6.3.0",
|
||||
"https": "^1.0.0",
|
||||
"turf": "^3.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -2048,6 +2049,11 @@
|
|||
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/https": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz",
|
||||
"integrity": "sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg=="
|
||||
},
|
||||
"node_modules/ieee754": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
|
||||
|
@ -4865,6 +4871,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz",
|
||||
"integrity": "sha512-4EC57ddXrkaF0x83Oj8sM6SLQHAWXw90Skqu2M4AEWENZ3F02dFJE/GARA8igO79tcgYqGrD7ae4f5L3um2lgg=="
|
||||
},
|
||||
"ieee754": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
"dependencies": {
|
||||
"@turf/boolean-point-in-polygon": "^6.0.1",
|
||||
"@turf/turf": "^6.3.0",
|
||||
"https": "^1.0.0",
|
||||
"turf": "^3.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -7,7 +7,15 @@ export class CountryCoder {
|
|||
private readonly _host: string;
|
||||
private readonly _downloadFunction: (url: string) => Promise<any>;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param host: where the sliced country files can be found
|
||||
* @param downloadFunction: a function which downloads and parsed the file as a JSON at the given URL. Useful to inject e.g. fetch
|
||||
*/
|
||||
constructor(host: string, downloadFunction?: (url: string) => Promise<any>) {
|
||||
if(!host.endsWith("/")){
|
||||
host += "/"
|
||||
}
|
||||
this._host = host;
|
||||
this._downloadFunction = downloadFunction;
|
||||
}
|
||||
|
@ -48,7 +56,7 @@ export class CountryCoder {
|
|||
|
||||
private async Fetch(z: number, x: number, y: number): Promise<number[] | string[] | any> {
|
||||
const path = `${z}.${x}.${y}.json`;
|
||||
const url = this._host + "/" + path;
|
||||
const url = this._host + path;
|
||||
if (this._downloadFunction !== undefined) {
|
||||
return this._downloadFunction(url)
|
||||
} else {
|
||||
|
|
9
test.ts
9
test.ts
|
@ -1,4 +1,6 @@
|
|||
import {CountryCoder} from "./src/CountryCoder";
|
||||
import https from "https";
|
||||
import Utils from "./generator/Utils";
|
||||
|
||||
console.log("Testing...")
|
||||
|
||||
|
@ -17,9 +19,12 @@ function expects(expected: string) {
|
|||
}
|
||||
|
||||
console.log("Hi world")
|
||||
const coder = new CountryCoder("https://pietervdvn.github.io/latlon2country");
|
||||
const url = "https://raw.githubusercontent.com/pietervdvn/MapComplete-data/main/latlon2country"
|
||||
const coder = new CountryCoder(url, url =>
|
||||
new Promise((resolve, reject) => Utils.Download(url, (result) => resolve(JSON.parse(result)), reject)));
|
||||
coder.GetCountryCodeFor(3.2, 51.2, expects("BE"))
|
||||
coder.GetCountryCodeFor(4.92119, 51.43995, expects("BE"))
|
||||
coder.GetCountryCodeFor(4.93189, 51.43552, expects("NL"))
|
||||
coder.GetCountryCodeFor(34.2581, 44.7536, expects("RU;UA"))
|
||||
coder.GetCountryCodeFor(-9.1330343, 38.7351593, expects("PT"))
|
||||
coder.GetCountryCodeFor(-9.1330343, 38.7351593, expects("PT"))
|
||||
coder.GetCountryCodeFor(3.1052356,50.7899285, expects("BE"))
|
BIN
tiles.zip
BIN
tiles.zip
Binary file not shown.
Loading…
Add table
Reference in a new issue