-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bad8e5b
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { readerFromStreamReader } from "https://deno.land/[email protected]/io/streams.ts" | ||
import * as csv from "https://deno.land/[email protected]/encoding/csv.ts" | ||
import dirname from "https://deno.land/x/denoname/mod/dirname.ts" | ||
|
||
|
||
// Script Settings | ||
|
||
const outName = "data.json"; // how the resulting file should be named | ||
const url = "http://www.fa-technik.adfc.de/code/opengeodb/DE.tab"; // where to download the csv from | ||
const minLevel = 6; // the minimum level to include | ||
|
||
|
||
// Useful Constants | ||
|
||
const dir = dirname(import.meta); | ||
const decoder = new TextDecoder(); | ||
const encoder = new TextEncoder(); | ||
|
||
|
||
// Download Data | ||
|
||
console.log("Downloading latest 'DE.tab' file from opengeodb"); | ||
console.time("Ellapsed time"); | ||
|
||
|
||
let res = await fetch(url) | ||
let reader = readerFromStreamReader(res.body!.getReader()) | ||
|
||
let data = await Deno.readAll(reader); | ||
|
||
|
||
// Parse Data as Array | ||
|
||
console.log("Parsing csv to array"); | ||
|
||
let string = decoder.decode(data); | ||
|
||
let arr = await csv.parse( string, { | ||
separator: "\t", | ||
lazyQuotes: true | ||
}) as string[][]; | ||
|
||
|
||
// Convert and Filter Data to required Format | ||
|
||
console.log("Converting array to objects"); | ||
|
||
interface GeoJsonPoint { | ||
type: "Point", | ||
coordinates: [number, number] | ||
} | ||
|
||
interface Doc { | ||
name: string, | ||
ascii: string, | ||
plz: string, | ||
location: GeoJsonPoint | ||
} | ||
|
||
let docArr: Doc[] = []; | ||
|
||
// get numerical indexes from first line in csv array | ||
const index = { | ||
name: arr[0].indexOf("name"), | ||
ascii: arr[0].indexOf("ascii"), | ||
plz: arr[0].indexOf("plz"), | ||
lat: arr[0].indexOf("lat"), | ||
lon: arr[0].indexOf("lon"), | ||
level: arr[0].indexOf("level") | ||
} | ||
|
||
// convert arrays to objects | ||
for (let i = 1; i < arr.length; i++) { | ||
const entry = arr[i]; | ||
|
||
let level = parseInt( entry[index.name] ); | ||
|
||
if (level < minLevel) continue; | ||
|
||
let lat = parseFloat( entry[index.lat] ); | ||
let lon = parseFloat( entry[index.lon] ); | ||
|
||
let loc: GeoJsonPoint = { | ||
type: "Point", | ||
coordinates: [ lat, lon ] | ||
} | ||
|
||
let doc: Doc = { | ||
name: entry[ index.name ], | ||
ascii: entry[ index.ascii ], | ||
plz: entry[ index.plz ], | ||
location: loc | ||
}; | ||
|
||
docArr.push(doc); | ||
} | ||
|
||
console.log("Stringifying to JSON"); | ||
|
||
data = encoder.encode( | ||
JSON.stringify(docArr) | ||
); | ||
|
||
console.log(`Writing file to local folder as '${outName}'`); | ||
|
||
await Deno.writeFile(dir + "data.json", data); | ||
|
||
console.log("Done!"); | ||
console.timeEnd("Ellapsed time"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Trans-db Tools | ||
|
||
## Content | ||
|
||
* [About](#about) | ||
* [GeoDbJson](#geodbjson) | ||
|
||
## About | ||
|
||
This repository conatins a collection of [deno](https://deno.land/) scripts, meant to aid the project in various ways. | ||
The scripts require no additional dependencies. | ||
|
||
Note, that due to how deno handles third-party modules, running a script for the first time requires an internet connection. | ||
|
||
## GeoDbJson | ||
|
||
### Info | ||
|
||
Donloads the 'DE.tab' entries from [OpenGeoDb](http://opengeodb.giswiki.org/wiki/OpenGeoDB) and converts them into a JSON format more suitable to the project. | ||
Only the used fields are imported, and the "lat" and "lon" fields are converted to a [GeoJSON](https://geojson.org/) point object. | ||
|
||
### Usage | ||
|
||
Use `deno run -A GeoDbJson/main.ts` to run the script. The resulting JSON document will appear in the same folder as the script. |