wfs = WebFeatureService(url=url) # Service provider print(wfs.identification.title) # Get WFS version print(wfs.version) # Available methods print([operation.name for operation in wfs.operations]) # Available data layers print(list(wfs.contents)) # Print all metadata of all layers for layer, meta in wfs.items(): print(meta.__dict__) # Get data from WFS # ----------------- # Fetch the last available layer (as an example) --> 'vaestoruutu:vaki2017_5km' layer = list(wfs.contents)[-1] # Specify the parameters for fetching the data params = dict(service='WFS', version="1.0.0", request='GetFeature', typeName=layer, outputFormat='json')
# print(layer_def.GetFieldCount()) # layer = None # OGR_end = time.clock() # print(OGR_end - OGR_start) OWS_start = time.clock() try: wfs = WebFeatureService(WFS_URL, version="2.0.0") except AttributeError: wfs = WebFeatureService(WFS_URL, version="1.1.0") print("\n\tGlobal: ", dir(wfs)) print(wfs.version) print(wfs.url) print(wfs.items()[0][1]) help(wfs.getSRS) print(wfs.timeout) print("\n\tIdentification: ", dir(wfs.identification)) print(wfs.identification.type) print(wfs.identification.title) print(wfs.identification.service) abstract = wfs.identification.abstract if abstract: print(abstract.encode('utf8')) print(wfs.identification.fees) print(wfs.identification.version) print(wfs.identification.keywords) print(wfs.identification.accessconstraints) print(wfs.identification.profiles)
def transformToCsv(nodeId, statBezirke): wfs = WebFeatureService(url='https://kommisdd.dresden.de/net3/public/ogcsl.ashx?NODEID='+str(nodeId)+'&Service=WFS&', version='2.0.0') print('NodeId: '+str(nodeId)+' '+wfs.identification.title) if containsUnnecessaryWord(wfs.identification.title): print('Skipped Node: '+str(nodeId)+' '+wfs.identification.title) return featureTypes = list(wfs.contents) featureTypeNames = {} for item in wfs.items(): featureTypeNames[item[0]] = item[1].title+' ('+item[0]+')' totalFeatures = len(wfs.items()) processedFeatures = 0 addedFeatures = 0 for featureType in featureTypes: print('Added: '+str(addedFeatures)+', Processed: '+str(processedFeatures)+', Total: '+str(totalFeatures)) try: processedFeatures +=1 root = ET.fromstring(wfs.getfeature(typename=featureType).getvalue()) featureTypeName = featureTypeNames[featureType] if containsUnnecessaryWord(featureTypeName): print('Skipped feature: '+featureTypeName) del featureTypeNames[featureType] continue if len(list(root)) > 2000: print('Skipped feature: '+featureTypeName) del featureTypeNames[featureType] continue elements = list(root.iter('{http://www.cardogis.com/kommisdd}'+featureType[4:])) if len(elements) == 0 or len(list(elements[0].iter('{http://www.opengis.net/gml/3.2}pos'))) == 0: print('Skipped feature: '+featureTypeName) del featureTypeNames[featureType] continue for element in elements: #print(element) #for stadtteil in neighbor.iter('{http://www.cardogis.com/kommisdd}stadtteil'): # print(stadtteil.text) for point in element.find('{http://www.cardogis.com/kommisdd}PrimaryGeometry'): #print(point) for pos in point.iter('{http://www.opengis.net/gml/3.2}pos'): #print(pos) for bezirk in statBezirke.values(): #print(bezirk) if featureTypeName not in bezirk.keys(): bezirk[featureTypeName] = 0 p = pos.text.split(' ') posPoint = Point(float(p[0]), float(p[1])) if posPoint.within(bezirk['polygon']): bezirk[featureTypeName] += 1 addedFeatures += 1 print('Added feature: '+featureTypeName) except: print('Error. Skipped feature: '+featureTypeName) del featureTypeNames[featureType] continue if len(featureTypeNames) > 0: with open('map/scripts/csv/'+wfs.identification.title+'.csv', mode='w', newline='') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=['Statistischer Bezirk']+list(featureTypeNames.values()),extrasaction='ignore', delimiter=';') writer.writeheader() writer.writerows(statBezirke.values()) return wfs.identification.title return False
class WFSDataServiceToReclineJS(): def __init__(self, url, version="1.0.0"): self.wfs = WebFeatureService(url, version=version) self.type = self.wfs.identification.type self.version = self.wfs.identification.version self.title = self.wfs.identification.title self.abstract = self.wfs.identification.abstract def get_layer_list(self): return list(self.wfs.contents) def get_single_layer(self, layer): theseLayers = self.get_layer_list() return [i for i in theseLayers if i == layer] def get_service_operations(self): thisWFS = self.wfs.operations return [op.name for op in thisWFS] def get_GET_feature_operation(self): operations = self.get_service_operations() return [i for i in operations if i.endswith("GetFeature")][0] def get_service_methods(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName(thisOperation).methods # def get_service_method_URL(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName( '{http://www.opengis.net/wfs}GetFeature' ).methods['{http://www.opengis.net/wfs}Get']['url'] def get_service_format_options(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName(thisOperation).formatOptions def get_GML_format_option(self, service_operation): formatOptions = self.get_service_format_options(service_operation) return [i for i in formatOptions if i.endswith("GML2")][0] def get_response(self, layer): thisLayer = self.get_single_layer(layer) thisOperation = self.get_GET_feature_operation() thisGML = self.get_GML_format_option(thisOperation) response = self.wfs.getfeature(typename=thisLayer) return response def get_items(self): return self.wfs.items() def hack_up_a_layer_name(self, data_dict): data = data_dict.get("resource") if data.get("layer_name"): return data.get("layer_name") elif data.get("layer"): return data.get("layer") elif data.get("layers"): return data.get("layers") else: try: layer_list = self.get_layers() return layer_list[0] except: return "Sorry, can't find a layer!" def make_recline_url(self, data_dict): data = data_dict thisLayer = self.hack_up_a_layer_name(data).lower() getMethod = self.get_GET_feature_operation() baseURL = self.get_service_method_URL(getMethod) baseURL += "&service=WFS&version=1.0.0&typeName=" baseURL += thisLayer return baseURL def MakeReclineJSON(self, data_dict): json_obj = [] attribs = [] data = data_dict gml_wfs = self.make_recline_url(data) source = ogr.Open(gml_wfs) print source layer = source.GetLayerByIndex(0) print layer for feature in layer: json_obj.append(feature.ExportToJson(as_object=True)) for i in json_obj: properties = i['properties'] properties.update(dict(geometry=i['geometry'])) attribs.append(properties) return attribs
# print(layer_def.GetFieldCount()) # layer = None # OGR_end = time.clock() # print(OGR_end - OGR_start) OWS_start = time.clock() try: wfs = WebFeatureService(WFS_URL, version="2.0.0") except AttributeError: wfs = WebFeatureService(WFS_URL, version="1.1.0") print(("\n\tGlobal: ", dir(wfs))) print(wfs.version) print(wfs.url) print(wfs.items()[0][1]) help(wfs.getSRS) print(wfs.timeout) print(("\n\tIdentification: ", dir(wfs.identification))) print(wfs.identification.type) print(wfs.identification.title) print(wfs.identification.service) abstract = wfs.identification.abstract if abstract: print(abstract.encode('utf8')) print(wfs.identification.fees) print(wfs.identification.version) print(wfs.identification.keywords) print(wfs.identification.accessconstraints) print(wfs.identification.profiles)
class WFSDataServiceToReclineJS(): def __init__(self, url, version="1.0.0"): self.wfs = WebFeatureService(url, version=version) self.type = self.wfs.identification.type self.version = self.wfs.identification.version self.title = self.wfs.identification.title self.abstract = self.wfs.identification.abstract def get_layer_list(self): return list(self.wfs.contents) def get_single_layer(self, layer): theseLayers = self.get_layer_list() return [i for i in theseLayers if i == layer] def get_service_operations(self): thisWFS = self.wfs.operations return [op.name for op in thisWFS] def get_GET_feature_operation(self): operations = self.get_service_operations() return [i for i in operations if i.endswith("GetFeature")][0] def get_service_methods(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName(thisOperation).methods # def get_service_method_URL(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName('{http://www.opengis.net/wfs}GetFeature').methods['{http://www.opengis.net/wfs}Get']['url'] def get_service_format_options(self, service_operation): thisWFS = self.wfs thisOperation = service_operation return thisWFS.getOperationByName(thisOperation).formatOptions def get_GML_format_option(self, service_operation): formatOptions = self.get_service_format_options(service_operation) return [i for i in formatOptions if i.endswith("GML2")][0] def get_response(self, layer): thisLayer = self.get_single_layer(layer) thisOperation = self.get_GET_feature_operation() thisGML = self.get_GML_format_option(thisOperation) response = self.wfs.getfeature(typename=thisLayer) return response def get_items(self): return self.wfs.items() def hack_up_a_layer_name(self, data_dict): data = data_dict.get("resource") if data.get("layer_name"): return data.get("layer_name") elif data.get("layer"): return data.get("layer") elif data.get("layers"): return data.get("layers") else: try: layer_list = self.get_layers() return layer_list[0] except: return "Sorry, can't find a layer!" def make_recline_url(self, data_dict): data = data_dict thisLayer = self.hack_up_a_layer_name(data).lower() getMethod = self.get_GET_feature_operation() baseURL = self.get_service_method_URL(getMethod) baseURL += "&service=WFS&version=1.0.0&typeName=" baseURL += thisLayer return baseURL def MakeReclineJSON(self, data_dict): json_obj = [] attribs = [] data = data_dict gml_wfs = self.make_recline_url(data) source = ogr.Open(gml_wfs) print source layer = source.GetLayerByIndex(0) print layer for feature in layer: json_obj.append(feature.ExportToJson(as_object=True)) for i in json_obj: properties = i['properties'] properties.update(dict(geometry=i['geometry'])) attribs.append(properties) return attribs