@@ -161,49 +161,53 @@ def geojson_to_geoxml(features, geoxml_path, itm=False):
161161
162162
163163def create_gis_data (gis_url , tmpdir ):
164- with open (os .path .join (tmpdir , 'gis.json' ), 'w' ) as f :
165- json .dump (fetch_gis_json (gis_url ), f , ensure_ascii = False , indent = 2 )
166- with open (os .path .join (tmpdir , 'gis.jsonlines' ), 'w' ) as f :
167- for feature in gis_query_geojson_iterate_all (gis_url ):
168- f .write (json .dumps (feature , ensure_ascii = False ) + '\n ' )
169- print ("Create geojson" )
170- with open (os .path .join (tmpdir , 'gis.geojson' ), 'w' ) as f :
171- f .write ('{"type": "FeatureCollection","features": [\n ' )
172- for i , feature in enumerate (iterate_gis_jsonlines (tmpdir )):
173- if i > 0 :
174- f .write (',\n ' )
175- f .write (' ' + json .dumps (feature , ensure_ascii = False ))
176- f .write (']}' )
177- print ("Create gis.itm.geojson" )
164+ if os .path .exists (os .path .join (tmpdir , 'gis.json' )):
165+ print ("WARNING: Using existing gis.json from tmpdir" )
166+ else :
167+ with open (os .path .join (tmpdir , 'gis.json' ), 'w' ) as f :
168+ json .dump (fetch_gis_json (gis_url ), f , ensure_ascii = False , indent = 2 )
169+ if os .path .exists (os .path .join (tmpdir , 'gis.jsonlines' )):
170+ print ("WARNING: Using existing gis.jsonlines from tmpdir" )
171+ else :
172+ with open (os .path .join (tmpdir , 'gis.jsonlines' ), 'w' ) as f :
173+ for feature in gis_query_geojson_iterate_all (gis_url ):
174+ f .write (json .dumps (feature , ensure_ascii = False ) + '\n ' )
175+ # create_geojson(tmpdir)
176+ # create_itm_geojson(tmpdir)
177+ create_shapefile (tmpdir )
178+ # feature_properties = create_csv_xlsx(tmpdir)
179+ # create_xml(feature_properties, tmpdir)
180+ # create_kml(tmpdir)
181+ # create_geoxml(tmpdir)
182+ # create_itm_geoxml(tmpdir)
183+
184+
185+ def create_itm_geoxml (tmpdir ):
186+ print ("Create gis.itm.geoxml" )
178187 try :
179- with open (os .path .join (tmpdir , 'gis.itm.geojson' ), 'w' ) as f :
180- f .write ('{"type": "FeatureCollection","features": [\n ' )
181- for i , feature in enumerate (iterate_gis_jsonlines (tmpdir )):
182- if i > 0 :
183- f .write (',\n ' )
184- feature = geojson_feature_to_itm (feature )
185- f .write (' ' + json .dumps (feature , ensure_ascii = False ))
186- f .write (']}' )
188+ geojson_to_geoxml (iterate_gis_jsonlines (tmpdir ), os .path .join (tmpdir , 'gis.itm.geoxml' ), itm = True )
187189 except FailedToConvertFeature as e :
188190 print (str (e ))
189- print ('failed to convert feature to itm' )
190- os .unlink (os .path .join (tmpdir , 'gis.itm.geojson' ))
191- print ("Create shapefile.zip" )
192- geojson_data = geopandas .read_file (os .path .join (tmpdir , 'gis.geojson' ))
193- os .makedirs (os .path .join (tmpdir , 'shapefile' ), exist_ok = True )
194- geojson_data .to_file (os .path .join (tmpdir , 'shapefile/gis.shp' ), driver = 'ESRI Shapefile' )
195- os .system (f'cd { tmpdir } && zip -r shapefile.zip shapefile' )
196- print ("Create gis.csv, gis.xlsx" )
197- feature_properties = set ()
198- for row in features_to_csv (iterate_gis_jsonlines (tmpdir )):
199- feature_properties .update (row .keys ())
200- DF .Flow (
201- features_to_csv (iterate_gis_jsonlines (tmpdir ), fields = feature_properties ),
202- DF .dump_to_path (os .path .join (tmpdir , 'csv' )),
203- DF .dump_to_path (os .path .join (tmpdir , 'xlsx' ), format = 'xlsx' )
204- ).process ()
205- shutil .copyfile (os .path .join (tmpdir , 'csv' , 'res_1.csv' ), os .path .join (tmpdir , 'gis.csv' ))
206- shutil .copyfile (os .path .join (tmpdir , 'xlsx' , 'res_1.xlsx' ), os .path .join (tmpdir , 'gis.xlsx' ))
191+ print ('failed to convert feature to geoxml' )
192+ os .unlink (os .path .join (tmpdir , 'gis.itm.geoxml' ))
193+
194+
195+ def create_geoxml (tmpdir ):
196+ print ("Create gis.geoxml" )
197+ try :
198+ geojson_to_geoxml (iterate_gis_jsonlines (tmpdir ), os .path .join (tmpdir , 'gis.geoxml' ))
199+ except FailedToConvertFeature as e :
200+ print (str (e ))
201+ print ('failed to convert feature to geoxml' )
202+ os .unlink (os .path .join (tmpdir , 'gis.geoxml' ))
203+
204+
205+ def create_kml (tmpdir ):
206+ print ("Create gis.kml" )
207+ geojson_to_kml (os .path .join (tmpdir , 'gis.geojson' ), os .path .join (tmpdir , 'gis.kml' ))
208+
209+
210+ def create_xml (feature_properties , tmpdir ):
207211 print ("Create gis.xml" )
208212 with open (os .path .join (tmpdir , 'gis.xml' ), 'w' ) as f :
209213 f .write ('<?xml version="1.0" encoding="UTF-8" ?>\n ' )
@@ -216,22 +220,57 @@ def create_gis_data(gis_url, tmpdir):
216220 f .write (f' <{ k } type="str">{ v } </{ k } >\n ' )
217221 f .write (' </item>\n ' )
218222 f .write ('</root>\n ' )
219- print ("Create gis.kml" )
220- geojson_to_kml (os .path .join (tmpdir , 'gis.geojson' ), os .path .join (tmpdir , 'gis.kml' ))
221- print ("Create gis.geoxml" )
222- try :
223- geojson_to_geoxml (iterate_gis_jsonlines (tmpdir ), os .path .join (tmpdir , 'gis.geoxml' ))
224- except FailedToConvertFeature as e :
225- print (str (e ))
226- print ('failed to convert feature to geoxml' )
227- os .unlink (os .path .join (tmpdir , 'gis.geoxml' ))
228- print ("Create gis.itm.geoxml" )
223+
224+
225+ def create_csv_xlsx (tmpdir ):
226+ print ("Create gis.csv, gis.xlsx" )
227+ feature_properties = set ()
228+ for row in features_to_csv (iterate_gis_jsonlines (tmpdir )):
229+ feature_properties .update (row .keys ())
230+ DF .Flow (
231+ features_to_csv (iterate_gis_jsonlines (tmpdir ), fields = feature_properties ),
232+ DF .dump_to_path (os .path .join (tmpdir , 'csv' )),
233+ DF .dump_to_path (os .path .join (tmpdir , 'xlsx' ), format = 'xlsx' )
234+ ).process ()
235+ shutil .copyfile (os .path .join (tmpdir , 'csv' , 'res_1.csv' ), os .path .join (tmpdir , 'gis.csv' ))
236+ shutil .copyfile (os .path .join (tmpdir , 'xlsx' , 'res_1.xlsx' ), os .path .join (tmpdir , 'gis.xlsx' ))
237+ return feature_properties
238+
239+
240+ def create_shapefile (tmpdir ):
241+ print ("Create shapefile.zip" )
242+ geojson_data = geopandas .read_file (os .path .join (tmpdir , 'gis.geojson' ))
243+ os .makedirs (os .path .join (tmpdir , 'shapefile' ), exist_ok = True )
244+ geojson_data .to_file (os .path .join (tmpdir , 'shapefile/gis.shp' ), driver = 'ESRI Shapefile' , encoding = 'utf-8' )
245+ os .system (f'cd { tmpdir } && zip -r shapefile.zip shapefile' )
246+
247+
248+ def create_itm_geojson (tmpdir ):
249+ print ("Create gis.itm.geojson" )
229250 try :
230- geojson_to_geoxml (iterate_gis_jsonlines (tmpdir ), os .path .join (tmpdir , 'gis.itm.geoxml' ), itm = True )
251+ with open (os .path .join (tmpdir , 'gis.itm.geojson' ), 'w' ) as f :
252+ f .write ('{"type": "FeatureCollection","features": [\n ' )
253+ for i , feature in enumerate (iterate_gis_jsonlines (tmpdir )):
254+ if i > 0 :
255+ f .write (',\n ' )
256+ feature = geojson_feature_to_itm (feature )
257+ f .write (' ' + json .dumps (feature , ensure_ascii = False ))
258+ f .write (']}' )
231259 except FailedToConvertFeature as e :
232260 print (str (e ))
233- print ('failed to convert feature to geoxml' )
234- os .unlink (os .path .join (tmpdir , 'gis.itm.geoxml' ))
261+ print ('failed to convert feature to itm' )
262+ os .unlink (os .path .join (tmpdir , 'gis.itm.geojson' ))
263+
264+
265+ def create_geojson (tmpdir ):
266+ print ("Create geojson" )
267+ with open (os .path .join (tmpdir , 'gis.geojson' ), 'w' ) as f :
268+ f .write ('{"type": "FeatureCollection","features": [\n ' )
269+ for i , feature in enumerate (iterate_gis_jsonlines (tmpdir )):
270+ if i > 0 :
271+ f .write (',\n ' )
272+ f .write (' ' + json .dumps (feature , ensure_ascii = False ))
273+ f .write (']}' )
235274
236275
237276def update_resource (target_instance_name , package , format_ , resource_name , file_path ):
@@ -268,9 +307,12 @@ def update_resource(target_instance_name, package, format_, resource_name, file_
268307
269308def operator (name , params ):
270309 gis_url = params ['gis_url' ]
271- target_instance_name = params ['target_instance_name' ]
272- target_package_id = params ['target_package_id' ]
273- target_organization_id = params ['target_organization_id' ]
310+ target_instance_name = params .get ('target_instance_name' )
311+ if target_instance_name :
312+ target_package_id = params ['target_package_id' ]
313+ target_organization_id = params ['target_organization_id' ]
314+ else :
315+ target_package_id , target_organization_id = None , None
274316 tmpdir = params .get ('tmpdir' )
275317 with tempdir (tmpdir ) as tmpdir :
276318 print ('starting gis_fetcher operator' )
@@ -281,30 +323,36 @@ def operator(name, params):
281323 gis_json = json .load (f )
282324 name = gis_json ['name' ]
283325 print (f'gis name={ name } ' )
284- package = ckan .package_show (target_instance_name , target_package_id )
285- if not package :
286- res = ckan .package_create (target_instance_name , {
287- 'name' : target_package_id ,
288- 'title' : name ,
289- 'owner_org' : target_organization_id
290- })
291- assert res ['success' ], str (res )
292- package = res ['result' ]
293- for format_ , resource_name , file_name in [
294- ('shapefile' , 'SHP' , 'shapefile.zip' ),
295- ('csv' , 'CSV' , 'gis.csv' ),
296- ('xlsx' , 'XLSX' , 'gis.xlsx' ),
297- ('geojson' , 'GeoJSON' , 'gis.geojson' ),
298- ('geojson' , 'GeoJSON-ITM' , 'gis.itm.geojson' ),
299- ('xml' , 'XML' , 'gis.xml' ),
300- ('kml' , 'KML' , 'gis.kml' ),
301- ('geoxml' , 'GeoXML' , 'gis.geoxml' ),
302- ('geoxml' , 'GeoXML-ITM' , 'gis.itm.geoxml' ),
303- ]:
304- update_resource (target_instance_name , package , format_ , resource_name , os .path .join (tmpdir , file_name ))
326+ if target_instance_name :
327+ package = ckan .package_show (target_instance_name , target_package_id )
328+ if not package :
329+ res = ckan .package_create (target_instance_name , {
330+ 'name' : target_package_id ,
331+ 'title' : name ,
332+ 'owner_org' : target_organization_id
333+ })
334+ assert res ['success' ], str (res )
335+ package = res ['result' ]
336+ for format_ , resource_name , file_name in [
337+ ('shapefile' , 'SHP' , 'shapefile.zip' ),
338+ ('csv' , 'CSV' , 'gis.csv' ),
339+ ('xlsx' , 'XLSX' , 'gis.xlsx' ),
340+ ('geojson' , 'GeoJSON' , 'gis.geojson' ),
341+ ('geojson' , 'GeoJSON-ITM' , 'gis.itm.geojson' ),
342+ ('xml' , 'XML' , 'gis.xml' ),
343+ ('kml' , 'KML' , 'gis.kml' ),
344+ ('geoxml' , 'GeoXML' , 'gis.geoxml' ),
345+ ('geoxml' , 'GeoXML-ITM' , 'gis.itm.geoxml' ),
346+ ]:
347+ update_resource (target_instance_name , package , format_ , resource_name , os .path .join (tmpdir , file_name ))
305348 print ('gis_fetcher operator completed successfully' )
306349
307350
351+ # test on ckan instance:
308352# python3 -m datacity_ckan_dgp.operators.gis_fetcher '{"gis_url": "https://gisserver.haifa.muni.il/arcgiswebadaptor/rest/services/PublicSite/Haifa_Eng_Public/MapServer/13", "target_instance_name": "LOCAL_DEVELOPMENT", "target_package_id": "yeudei_karka", "target_organization_id": "muni", "tmpdir": ".data/gis_fetcher_tmpdir"}'
353+
354+ # test locally without ckan instance:
355+ # python3 -m datacity_ckan_dgp.operators.gis_fetcher '{"gis_url": "https://gisserver.haifa.muni.il/arcgiswebadaptor/rest/services/PublicSite/Haifa_Eng_Public/MapServer/13", "tmpdir": ".data/gis_fetcher_tmpdir"}'
356+
309357if __name__ == '__main__' :
310358 operator ('_' , json .loads (sys .argv [1 ]))
0 commit comments