Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectronicBlueberry committed Jan 15, 2021
0 parents commit bad8e5b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
1 change: 1 addition & 0 deletions GeoDbJson/data.json

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions GeoDbJson/main.ts
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");
24 changes: 24 additions & 0 deletions Readme.md
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.

0 comments on commit bad8e5b

Please sign in to comment.