@@ -42,6 +42,7 @@ <h2>Convert lat-long string from ona.io to KML</h2>
42
42
< script >
43
43
44
44
function text2kml ( ) {
45
+ // TO DO : FIX A BUG! sample data from Philip created some "undefined" stuff at end.
45
46
46
47
/*
47
48
input : 18.530663 73.875515 0 0;18.534935 73.874657 0 0;18.534925 73.87626 0 0;18.531995 73.876925 0 0;18.530663 73.875515 0 0
@@ -64,11 +65,17 @@ <h2>Convert lat-long string from ona.io to KML</h2>
64
65
var longlatarray = [ ] ;
65
66
66
67
for ( coord in inputarray ) {
67
- var container = inputarray [ coord ] . split ( ' ' ) ;
68
+ // data cleaning: zap everything that is not 0-9 (.) (;) or (space). from https://stackoverflow.com/a/4460306/4355695
69
+ var latlonstring = inputarray [ coord ] . replace ( / [ ^ 0 - 9 . ; ] / g, "" ) . trim ( ) ;
70
+ var container = latlonstring . split ( ' ' ) ;
68
71
console . log ( container ) ;
69
- var rectified = container [ 1 ] + ',' + container [ 0 ] + ',' + container [ 2 ] ;
70
- //console.log(rectified);
71
- longlatarray . push ( rectified ) ;
72
+ if checklatlng ( container [ 0 ] , container [ 1 ] ) {
73
+ // check for valid lat-long values, push in only if valid.
74
+ var rectified = container [ 1 ] + ',' + container [ 0 ] + ',' + container [ 2 ] ;
75
+ //console.log(rectified);
76
+ longlatarray . push ( rectified ) ;
77
+ }
78
+
72
79
}
73
80
74
81
outputstring = '<Placemark>\n<name>Shape' + shapecounter + '</name>\n<MultiGeometry><Polygon><outerBoundaryIs><LinearRing>\n<coordinates>' + longlatarray . join ( ' ' ) + '</coordinates>\n</LinearRing></outerBoundaryIs></Polygon></MultiGeometry>\n</Placemark>' ;
@@ -127,6 +134,19 @@ <h2>Convert lat-long string from ona.io to KML</h2>
127
134
*/
128
135
}
129
136
137
+ // brought in from static-gtfs-manager
138
+ function checklatlng ( lat , lon ) {
139
+ if ( typeof lat == 'number' && typeof lon == 'number' &&
140
+ ! isNaN ( lat ) && ! isNaN ( lon ) ) {
141
+ //console.log(lat,lon,'is valid');
142
+ // again, check if they're in 90 -90 etc
143
+ if ( ( - 90 <= lat <= 90 ) && ( - 180 <= lon <= 180 ) ) return true ;
144
+ }
145
+
146
+ // no need of else.. if it fails anywhere above, default return should be false
147
+ //console.log(lat,lon,'is not valid');
148
+ return false ;
149
+ }
130
150
</ script >
131
151
</ body >
132
152
</ html >
0 commit comments