-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocate.js
190 lines (155 loc) · 4.98 KB
/
geolocate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
// includes
const Fs = require( 'mz/fs' ),
Parser = require( 'papaparse' );
// constants
const ADD_COL_LAT = 1,
ADD_COL_LON = 2,
ADD_COL_STREET = 3,
ADD_COL_NUMBER = 4,
SRC_COL_W = 2,
SRC_COL_GT = 3,
SRC_COLCOUNT = 4,
STOPWORDS = ['unbekannt'],
REMOVALS = [
/\([^\)]*\)/gi, // additions in parenthesis
/u\. a\./gi,
/Str\./gi, /Straße/gi,
/\d+\./gi,
];
async function run(){
// Create address lookup
const addLookup = {};
let data = await Fs.readFile( __dirname + '/data/adressen.csv', 'utf8' );
data.split( '\n' )
.map( line => line.trim() )
.filter( line => line != '' )
.map( line => line.split( ',' ) )
.forEach( line => {
line[ ADD_COL_STREET ] = line[ ADD_COL_STREET ].replace( /Str\./gi, '' ).trim();
addLookup[ line[ ADD_COL_STREET ] ] = addLookup[ line[ ADD_COL_STREET ] ] || {};
addLookup[ line[ ADD_COL_STREET ] ][ line[ ADD_COL_NUMBER ] ] = {
lat: line[ ADD_COL_LAT ],
lon: line[ ADD_COL_LON ]
}
});
// parse entries
data = await Fs.readFile( __dirname + '/data/source.tsv', 'utf8' );
let input = Parser.parse( data, { delimiter: '\t' } ).data;
/*
let input = data.split( '\n' )
.map( line => {
// remove start end stopcharacters
line = trimChar( line.trim(), [' ',':'] )
// get numbers out of parenthesis
line = line.replace( /\((\d{1,2})\)/gi, '$1' );
return line;
});
*/
// remove lines of wrong column count
input = input.filter( (line, rowNumber) => {
if( line.length != SRC_COLCOUNT ) {
console.log( 'invalid line (' + rowNumber + ') column count', line.length );
return false;
} else {
return true;
}
});
// some pre processing of address columns
input.forEach( (line) => {
// remove start end stopcharacters
line[ SRC_COL_W ] = trimChar( line[ SRC_COL_W ].trim(), [' ',':'] )
// get numbers out of parenthesis
line[ SRC_COL_W ] = line[ SRC_COL_W ].replace( /\((\d{1,2})\)/gi, '$1' );
line[ SRC_COL_GT ] = line[ SRC_COL_GT ] || '';
// remove start end stopcharacters
line[ SRC_COL_GT ] = trimChar( line[ SRC_COL_GT ].trim(), [' ',':'] )
// get numbers out of parenthesis
line[ SRC_COL_GT ] = line[ SRC_COL_GT ].replace( /\((\d{1,2})\)/gi, '$1' );
});
// process
let nohit = 0, hit = 0;
input.forEach( entry => {
// W cell
let add = parse( entry[ SRC_COL_W ] ),
coord = null,
usedAddr = null;
if( add && (add.length == 1) ) {
({ coord, usedAddr } = match( add[0] ));
}
entry.push( coord );
// GT cell
add = parse( entry[ SRC_COL_GT ] );
coord = null;
usedAddr = null;
if( add && (add.length == 1) ) {
({ coord, usedAddr } = match( add[0] ));
}
entry.push( usedAddr );
entry.push( coord );
})
let output = Parser.unparse( input, { delimiter: '\t' } );
await Fs.writeFile( __dirname + '/data/res_geoloc.tsv', output );
console.log( 'hits:', hit );
console.log( 'nohits:', nohit );
console.log( 'total:', (hit+nohit),'/', input.length );
/* ------------------------- Functions ------------------------- */
/*
* parse one address string
*/
function parse( entry ) {
// try to parse the string
let comp = Parser.parse( entry,{
delimiter: function(input) { return input.includes( ';' ) ? ';' : ',' }
});
// 0nly parsables
if( !('data' in comp) || (comp.data.length < 1) ){
nohit++;
return;
}
comp = comp.data[0].map( entry => entry.trim() ).filter( entry => entry != '' );
// skip stopwords
if( (comp.length == 1) && STOPWORDS.includes( input ) ){
nohit++;
return;
}
return comp;
}
/*
* try to match a single entry to the address list
*/
function match( input, entry ) {
// removals
REMOVALS.forEach( (r) => {
input = input.replace( r, '' );
});
input = input.replace( /\(.*\)/, '' );
// try to identify name and number
const name = input.replace( /\d/gui, '' ).trim(),
number = input.replace( /[^0-9]/gi, '' ).trim();
// try to find
if( (name in addLookup) && (number in addLookup[name]) ){
// found it
hit++;
return {
coord: '@' + addLookup[name][number].lat + '/' + addLookup[name][number].lon,
usedAddr: input.trim()
};
} else {
nohit++;
return { coord: '', usedAddr: '' };
}
}
}
run().then()
.catch( (e) => console.log(e) );
// https://stackoverflow.com/a/26156806/1169798
function trimChar(string, charToRemove) {
while(charToRemove.includes( string.charAt(0) )) {
string = string.substring(1);
}
while(charToRemove.includes( string.charAt(string.length-1) ) ) {
string = string.substring(0,string.length-1);
}
return string;
}