@@ -75,15 +75,19 @@ function add_zaehlstellen(coords_json)
75
75
76
76
//remove current coordinates, if existing
77
77
78
-
79
78
// save the current Selection to global variable selectedOptions, so they can only be changed with the apply button
80
79
var idField = document . getElementById ( "coordIDSelect" ) . value . split ( "," ) ; // array, because it might be nested
81
- var coordsField = document . getElementById ( "coordSelect " ) . value . split ( "," ) ; // array, because it might be nested
80
+ var coordsField = document . getElementById ( "xSelect " ) . value . split ( "," ) ; // array, because it might be nested
82
81
var epsgField = document . getElementById ( "epsgInput" ) . value ;
83
82
selectedOptions . coordID = idField ;
84
83
selectedOptions . coordField = coordsField ;
85
84
selectedOptions . epsg = epsgField ;
86
85
86
+ // if coordinates are csv, make an aproppriate JSON with selected x and y coordinates and Match-ID as property
87
+ if ( currentFiles . CoordsFileType === "csv" ) {
88
+ coords_json = csvToGeoJSON ( window . csv ) ;
89
+ }
90
+
87
91
// If EPSG is not empty or 4326, the data has to be reprojected. get the .wkt from epsg.io api
88
92
var responseString = "" ;
89
93
if ( epsgField !== "4326" && epsgField !== "" )
@@ -107,10 +111,10 @@ function add_zaehlstellen(coords_json)
107
111
xhttp . send ( ) ;
108
112
}
109
113
//eval(responseString);
110
-
114
+ console . log ( ol . proj . get ( 'EPSG:' + epsgField ) ) ;
111
115
ZaehlstellenPoints = new ol . layer . Vector ( {
112
116
source : new ol . source . Vector ( {
113
- features : ( new ol . format . GeoJSON ( ) ) . readFeatures ( coords_json , {
117
+ features : ( new ol . format . GeoJSON ( { defaultDataProjection : 'EPSG:4326' } ) ) . readFeatures ( coords_json , {
114
118
//dataProjection: proj4('EPSG:' + epsgField), //e.g.: proj4('EPSG:32633');
115
119
dataProjection : ol . proj . get ( 'EPSG:' + epsgField ) , //e.g.: proj4('EPSG:32633');
116
120
//dataProjection: ol.proj.ProjectionLike('EPSG:' + epsgField), //e.g.: proj4('EPSG:32633');
@@ -161,7 +165,7 @@ function add_zaehlstellen(coords_json)
161
165
if ( typeof ( selectedOptions . dateField ) !== "undefined" ) { updateInput ( 0 , false , false ) ; } ;
162
166
document . getElementById ( "sliderDiv" ) . style . display = 'inline-block' ;
163
167
}
164
- addPopups ( ) // add eventhandler for clicks to show popups
168
+ // addPopups() // add eventhandler for clicks to show popups
165
169
}
166
170
167
171
@@ -237,12 +241,14 @@ function add_zaehlstellen(coords_json)
237
241
if ( r == true ) {
238
242
console . log ( "Override File" ) ;
239
243
// now clear all old options from Coords- and Match-ID-Selection
240
- var select = document . getElementById ( "coordSelect " ) ;
244
+ var select = document . getElementById ( "xSelect " ) ;
241
245
var select2 = document . getElementById ( "coordIDSelect" ) ;
246
+ var select3 = document . getElementById ( "ySelect" ) ;
242
247
var length = select . options . length ; // the 2 selects should have same options
243
248
for ( i = 0 ; i < length ; i ++ ) {
244
249
select . options [ 0 ] = null ;
245
250
select2 . options [ 0 ] = null ;
251
+ select3 . options [ 0 ] = null ;
246
252
}
247
253
}
248
254
else {
@@ -262,18 +268,28 @@ function add_zaehlstellen(coords_json)
262
268
coords_json = { } ;
263
269
var reader = new FileReader ( ) ; // to read the FileList object
264
270
reader . onload = function ( event ) { // Reader ist asynchron, wenn reader mit operation fertig ist, soll das hier (JSON.parse) ausgeführt werden, sonst ist es noch null
271
+ var columnNames = [ ] ;
265
272
if ( f . name . substr ( f . name . length - 3 ) === "csv" ) { // check if filetiype is csv
266
- coords_json = csvToGeoJSON ( reader . result ) ;
273
+ currentFiles . CoordsFileType = "csv" ;
274
+ columnNames = getColumnNames ( reader . result ) ;
275
+ window . csv = reader . result ; // temporary save reader.result into global variable, until geoJSON can be created with user-inputs
276
+ askFields2 ( columnNames , 1 ) ;
267
277
}
268
- else {
278
+ else if ( f . name . substr ( f . name . length - 4 ) === "json" ) {
279
+ currentFiles . CoordsFileType = "JSON" ;
269
280
coords_json = JSON . parse ( reader . result ) ;
281
+ askFields2 ( columnNames , 1 ) ;
282
+ }
283
+ else {
284
+ alert ( "Unrecognized Filetype. Please Check your input (only .csv or .json allowed)" ) ;
270
285
}
286
+
271
287
document . getElementById ( "hideCoordSelection" ) . style . visibility = "visible" ;
272
288
document . getElementById ( "choseFieldDiv1" ) . style . visibility = "visible" ;
273
289
document . getElementById ( "renderCoordinatesButton" ) . style . visibility = "visible" ;
274
290
document . getElementById ( "hideSelectionHolder" ) . style . visibility = "visible" ;
275
291
276
- askFields ( coords_json . features [ 0 ] , 1 ) ; // only first feature is needed for property names
292
+ // askFields(coords_json.features[0], 1); // only first feature is needed for property names
277
293
document . getElementById ( "renderCoordinatesButton" ) . addEventListener ( 'click' , function ( ) { add_zaehlstellen ( coords_json ) ; } , false ) ;
278
294
//console.log('added Event Listener to apply button');
279
295
//add_zaehlstellen(coords_json);
@@ -283,12 +299,23 @@ function add_zaehlstellen(coords_json)
283
299
document . getElementById ( 'list_coords' ) . innerHTML = '<ul style="margin: 0px;">' + output . join ( '' ) + '</ul>' ;
284
300
}
285
301
302
+ function getColumnNames ( csv ) {
303
+ var lines = csv . split ( / \r | \n / ) ;
304
+ return lines [ 0 ] . split ( "," ) ; //returns array with column names
305
+ }
306
+
286
307
// convert .csv to geoJSON
287
308
function csvToGeoJSON ( csv ) { //csv = reader.result
288
- var lines = csv . split ( / \r | \n / ) ;
289
- //line = lines.split("");
290
- //var result = [];
291
- var headers = lines [ 0 ] . split ( "," ) ;
309
+ var lines = csv . split ( / [ \r \n ] + / ) ; // split for windows and mac csv (newline or carriage return)
310
+ var headers = lines [ 0 ] . split ( "," ) ; //not needed?
311
+ var matchID = document . getElementById ( "coordIDSelect" ) . value ;
312
+ var xColumn = document . getElementById ( "xSelect" ) . value ;
313
+ var yColumn = document . getElementById ( "ySelect" ) . value ;
314
+
315
+ // get the positions of the seleted columns in the header
316
+ var positionMatchID = headers . indexOf ( matchID ) ;
317
+ var positionX = headers . indexOf ( xColumn ) ;
318
+ var positionY = headers . indexOf ( yColumn ) ;
292
319
293
320
var obj_array = [ ]
294
321
for ( var i = 1 ; i < lines . length ; i ++ ) {
@@ -297,9 +324,9 @@ function add_zaehlstellen(coords_json)
297
324
//for(var j=0;j<headers.length;j++){
298
325
json_obj [ "geometry" ] = {
299
326
"type" : "Point" ,
300
- "coordinates" : [ currentline [ 1 ] , currentline [ 2 ] ] } ;
327
+ "coordinates" : [ currentline [ positionX ] , currentline [ positionY ] ] } ;
301
328
json_obj [ "properties" ] = { } ;
302
- json_obj [ "properties" ] [ headers [ 0 ] ] = currentline [ 0 ] ; // get the name of zaehlstellen variable
329
+ json_obj [ "properties" ] [ matchID ] = currentline [ positionMatchID ] ; // get the name of zaehlstellen variable
303
330
obj_array . push ( json_obj ) ;
304
331
} ;
305
332
@@ -357,7 +384,7 @@ function add_zaehlstellen(coords_json)
357
384
document . getElementById ( "choseFieldDiv2" ) . style . visibility = "visible" ;
358
385
document . getElementById ( "hideSelectionHolder" ) . style . visibility = "visible" ;
359
386
360
- askFields ( zaehlstellen_data [ 0 ] , 2 ) ; // only first feature is needed for property names
387
+ askFields2 ( zaehlstellen_data [ 0 ] , 2 ) ; // only first feature is needed for property names
361
388
document . getElementById ( "renderDataButton" ) . addEventListener ( 'click' , function ( ) { applyDate ( ) ; } , false ) ;
362
389
} ;
363
390
reader . readAsText ( f ) ;
@@ -912,14 +939,109 @@ function autoPlay(){
912
939
} ;
913
940
914
941
//----------Populating Selections for ID and Coordinates after Drag&Drop--------------------------------------------------
915
- function askFields ( first_feature , option ) {
942
+ function askFields2 ( columnNames , option ) {
943
+ // @columnNames : Array with column Names as Strings
916
944
// @option :
917
945
// 1: Coordinates
918
946
// 2: Data
947
+
948
+
919
949
if ( option == 1 ) { // if Coordinates, then show/hide Coordinates, else Data selection
920
950
if ( selectionStatus . coords == false ) { // only show/hide when not already shown, happens when re-dragging data into drop-field
921
951
showCoordsSelection ( ) ;
922
952
}
953
+ }
954
+ else {
955
+ if ( selectionStatus . date == false ) {
956
+ showDateSelection ( ) ;
957
+ }
958
+ }
959
+
960
+ switch ( option )
961
+ {
962
+ case 1 : // = coordinates
963
+ {
964
+ if ( currentFiles . CoordsFileType === "csv" ) {
965
+ var coordIDSelection = document . getElementById ( 'coordIDSelect' ) ;
966
+ var coordXSelection = document . getElementById ( 'xSelect' ) ;
967
+ var coordYSelection = document . getElementById ( 'ySelect' ) ;
968
+
969
+ // clear all existing options
970
+ coordIDSelection . options . length = 0 ;
971
+ coordXSelection . options . length = 0 ;
972
+ coordYSelection . options . length = 0 ;
973
+
974
+ coordXSelection . disabled = false ; // enable x- and y-coordinate Selection, if it got deselected before
975
+ coordYSelection . disabled = false ;
976
+
977
+ var headerLength = columnNames . length ;
978
+ for ( i = 0 ; i < headerLength ; i ++ ) {
979
+ var opt = document . createElement ( "option" ) ;
980
+ opt . value = columnNames [ i ] ;
981
+ opt . innerHTML = columnNames [ i ] ;
982
+ coordIDSelection . appendChild ( opt ) ;
983
+ var opt2 = opt . cloneNode ( true ) ; // clone Options for other Selection
984
+ coordXSelection . appendChild ( opt2 ) ;
985
+ var opt3 = opt . cloneNode ( true ) ; // clone Options for other Selection
986
+ coordYSelection . appendChild ( opt3 ) ;
987
+ }
988
+ }
989
+ else if ( currentFiles . CoordsFileType === "JSON" ) {
990
+
991
+ var propertyNames = Object . getOwnPropertyNames ( coords_json . features [ 0 ] . properties ) // array of properties of GeoJSON
992
+ var coordIDSelection = document . getElementById ( 'coordIDSelect' ) ;
993
+
994
+ // clear all existing options
995
+ coordIDSelection . options . length = 0 ;
996
+
997
+ var propertyNamesLength = propertyNames . length ;
998
+ for ( i = 0 ; i < propertyNamesLength ; i ++ ) {
999
+ var opt = document . createElement ( "option" ) ;
1000
+ opt . value = propertyNames [ i ] ;
1001
+ opt . innerHTML = propertyNames [ i ] ;
1002
+ coordIDSelection . appendChild ( opt ) ;
1003
+ }
1004
+ document . getElementById ( "xSelect" ) . disabled = true ; // disable x- and y-coordinate Selection, because geometry-property is standardized
1005
+ document . getElementById ( "ySelect" ) . disabled = true ;
1006
+ }
1007
+ // Behavior of sliding Div and show/hide-Buttons depending on option
1008
+ document . getElementById ( "hideCoordSelection" ) . style . display = "inline-block" ;
1009
+ document . getElementById ( "hideCoordSelection" ) . innerHTML = "△" ;
1010
+ } // end of case 1 (=coordinates-Json)
1011
+ break ;
1012
+
1013
+ case 2 : //(= Data file)
1014
+ {
1015
+ var propertyNames = Object . getOwnPropertyNames ( zaehlstellen_data [ 0 ] ) // array of properties of GeoJSON
1016
+ var dateSelection = document . getElementById ( 'dateSelect' ) ;
1017
+
1018
+ // clear all existing options
1019
+ dateSelection . options . length = 0 ;
1020
+
1021
+ for ( i = 0 ; i < propertyNames . length ; i ++ ) {
1022
+ var opt = document . createElement ( "option" ) ;
1023
+ opt . value = propertyNames [ i ] ;
1024
+ opt . innerHTML = propertyNames [ i ] ;
1025
+ dateSelection . appendChild ( opt ) ;
1026
+ }
1027
+
1028
+ document . getElementById ( "hideDataSelection" ) . style . display = "inline-block" ;
1029
+ document . getElementById ( "hideDataSelection" ) . innerHTML = "△" ;
1030
+ } // end of case 2 (=Data-json)
1031
+ break ;
1032
+
1033
+ } // end of switch
1034
+ }
1035
+
1036
+ //----------Populating Selections for ID and Coordinates after Drag&Drop--------------------------------------------------
1037
+ function askFields ( first_feature , option ) {
1038
+ // @option :
1039
+ // 1: Coordinates
1040
+ // 2: Data
1041
+ if ( option == 1 ) { // if Coordinates, then show/hide Coordinates, else Data selection
1042
+ if ( selectionStatus . coords == false ) { // only show/hide when not already shown, happens when re-dragging data into drop-field
1043
+ showCoordsSelection ( ) ;
1044
+ }
923
1045
}
924
1046
else {
925
1047
if ( selectionStatus . date == false ) {
@@ -932,7 +1054,7 @@ function askFields(first_feature, option){
932
1054
case 1 : // = coordinates-json
933
1055
{
934
1056
var coordIDSelection = document . getElementById ( 'coordIDSelect' ) ;
935
- var coordSelection = document . getElementById ( 'coordSelect ' ) ;
1057
+ var coordSelection = document . getElementById ( 'xSelect ' ) ;
936
1058
index = 0 ;
937
1059
Object . keys ( first_feature ) . forEach ( function ( prop ) { // prop = property name
938
1060
//console.log(prop);
@@ -1021,8 +1143,8 @@ function showCoordsSelection(){
1021
1143
else {
1022
1144
document . getElementById ( "hideCoordSelection" ) . innerHTML = "▽" ;
1023
1145
document . getElementById ( "hideCoordSelection" ) . style . backgroundColor = "#A4C4E8" ;
1024
- document . getElementById ( 'choseFieldDiv1' ) . style . transform = "translateY(-45px )" ;
1025
- document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-210px )" ;
1146
+ document . getElementById ( 'choseFieldDiv1' ) . style . transform = "translateY(-83px )" ;
1147
+ document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-248px )" ;
1026
1148
selectionStatus . coords = false ;
1027
1149
}
1028
1150
}
@@ -1039,15 +1161,17 @@ function showDateSelection(){
1039
1161
if ( selectionStatus . date == false ) {
1040
1162
document . getElementById ( "hideDataSelection" ) . innerHTML = "△" ;
1041
1163
document . getElementById ( "hideDataSelection" ) . style . backgroundColor = "#4A74AA" ;
1042
- document . getElementById ( 'choseFieldDiv2' ) . style . transform = "translateY(23px)" ;
1043
- document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-140px)" ;
1164
+ //document.getElementById('choseFieldDiv2').style.transform = "translateY(23px)";
1165
+ //document.getElementById("menuBelowSelection").style.transform = "translateY(-140px)";
1166
+ document . getElementById ( 'choseFieldDiv2' ) . style . transform = "translateY(-17px)" ;
1167
+ document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-180px)" ;
1044
1168
selectionStatus . date = true ;
1045
1169
}
1046
1170
else {
1047
1171
document . getElementById ( "hideDataSelection" ) . innerHTML = "▽" ;
1048
1172
document . getElementById ( "hideDataSelection" ) . style . backgroundColor = "#A4C4E8" ;
1049
- document . getElementById ( 'choseFieldDiv2' ) . style . transform = "translateY(-45px )" ;
1050
- document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-205px )" ;
1173
+ document . getElementById ( 'choseFieldDiv2' ) . style . transform = "translateY(-84px )" ;
1174
+ document . getElementById ( "menuBelowSelection" ) . style . transform = "translateY(-247px )" ;
1051
1175
selectionStatus . date = false ;
1052
1176
}
1053
1177
}
0 commit comments