def exportResults(geocodeResultObject, out_fc): """exports the geocode results to feature class Required: geocodeResultObject -- results from geocode operation, must be of type GeocodeResult. out_fc -- full path to output shapefile """ if isinstance(geocodeResultObject, GeocodeResult): handler = GeocodeHandler(geocodeResultObject) if not handler.results: print('Geocoder returned 0 results! Did not create output') return None # create shapefile w = shp_helper.shp('POINT', out_fc) for field in handler.fields: w.add_field(field.name, field.type, 254) # add values for values in handler.formattedResults: w.add_row(values[0], values[1:]) w.save() # project shapefile project(out_fc, handler.spatialReference) print('Created: "{}"'.format(out_fc)) return out_fc else: raise TypeError('{} is not a {} object!'.format(geocodeResultObject, GeocodeResult))
def layer_to_fc(self, out_fc, sr=None, where='1=1', params={}, flds='*', records=None, get_all=False): """Method to export a feature class from a service layer Required: out_fc -- full path to output feature class Optional: sr -- output spatial refrence (WKID) where -- optional where clause params -- dictionary of parameters for query flds -- list of fields for fc. If none specified, all fields are returned. Supports fields in list [] or comma separated string "field1,field2,.." records -- number of records to return. Default is none, will return maxRecordCount get_all -- option to get all records. If true, will recursively query REST endpoint until all records have been gathered. Default is False. """ if self.type == 'Feature Layer': if not flds: flds = '*' if flds: if flds == '*': fields = self.fields else: if isinstance(flds, list): pass elif isinstance(flds, basestring): flds = flds.split(',') fields = [f for f in self.fields if f.name in flds] # make new feature class if not sr: sr = self.spatialReference g_type = G_DICT[self.geometryType] # add all fields w = shp_helper.shp(g_type, out_fc) field_map = [] for fld in fields: if fld.type not in [OID, SHAPE] + SKIP_FIELDS.keys(): if not 'shape' in fld.name.lower(): field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] field_length= str(fld.length) w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) # search cursor to write rows s_fields = ['SHAPE@'] + [f[0] for f in field_map] cursor = self.cursor(s_fields, where, records, params, get_all) for row in cursor.rows(): w.add_row(row[0], row[1:]) w.save() print 'Created: "{0}"'.format(out_fc) # write projection file project(out_fc, sr) return out_fc else: print 'Cannot convert layer: "{0}" to Feature Layer, Not a vector layer!'.format(self.name)
def exportResults(self, geocodeResultObject, out_fc): """exports the geocode results to feature class Required: geocodeResultObject -- results from geocode operation, must be of type GeocodeResult. out_fc -- full path to output shapefile """ handler = GeocodeHandler(geocodeResultObject) if not handler.results: print "Geocoder returned 0 results! Did not create output" return None # create shapefile w = shp_helper.shp("POINT", out_fc) for field in handler.fields: w.add_field(field.name, field.type, 254) # add values for values in handler.formattedResults: w.add_row(values[0], values[1:]) w.save() # project shapefile project(out_fc, handler.spatialReference) print 'Created: "{}"'.format(out_fc) return out_fc
def exportResults(geocodeResultObject, out_fc): """exports the geocode results to feature class Required: geocodeResultObject -- results from geocode operation, must be of type GeocodeResult. out_fc -- full path to output shapefile """ if isinstance(geocodeResultObject, GeocodeResult): handler = GeocodeHandler(geocodeResultObject) if not handler.results: print('Geocoder returned 0 results! Did not create output') return None # create shapefile w = shp_helper.shp('POINT', out_fc) for field in handler.fields: w.add_field(field.name, field.type, 254) # add values for values in handler.formattedResults: w.add_row(values[0], values[1:]) w.save() # project shapefile project(out_fc, handler.spatialReference) print('Created: "{}"'.format(out_fc)) return out_fc else: raise TypeError('{} is not a {} object!'.format( geocodeResultObject, GeocodeResult))
def layer_to_fc(self, out_fc, fields='*', where='1=1', records=None, params={}, get_all=False, sr=None): """Method to export a feature class from a service layer Required: out_fc -- full path to output feature class Optional: sr -- output spatial refrence (WKID) where -- optional where clause params -- dictionary of parameters for query flds -- list of fields for fc. If none specified, all fields are returned. Supports fields in list [] or comma separated string "field1,field2,.." records -- number of records to return. Default is none, will return maxRecordCount get_all -- option to get all records. If true, will recursively query REST endpoint until all records have been gathered. Default is False. """ if self.type == 'Feature Layer': if not fields: fields = '*' if fields == '*': _fields = self.fields else: if isinstance(fields, basestring): fields = fields.split(',') _fields = [f for f in self.fields if f.name in fields] # make new feature class if not sr: sr = self.spatialReference g_type = G_DICT[self.geometryType] # add all fields w = shp_helper.shp(g_type, out_fc) field_map = [] for fld in _fields: if fld.type not in [OID, SHAPE] + SKIP_FIELDS.keys(): if not any(['shape_' in fld.name.lower(), 'shape.' in fld.name.lower(), '(shape)' in fld.name.lower(), 'objectid' in fld.name.lower(), fld.name.lower() == 'fid']): field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) # search cursor to write rows s_fields = [f[0] for f in field_map] if not self.SHAPE.name in s_fields and 'SHAPE@' not in s_fields: s_fields.append('SHAPE@') query_resp = self.cursor(s_fields, where, records, params, get_all).response return exportFeatureSet(out_fc, query_resp, outSR=sr) else: print('Cannot convert layer: "{0}" to Feature Layer, Not a vector layer!'.format(self.name))
def exportFeatureSet(out_fc, feature_set, outSR=None): """export features (JSON result) to shapefile or feature class Required: out_fc -- output feature class or shapefile feature_set -- JSON response (feature set) obtained from a query Optional: outSR -- optional output spatial reference. If none set, will default to SR of result_query feature set. """ # validate features input (should be list or dict, preferably list) if isinstance(feature_set, basestring): try: feature_set = json.loads(feature_set) except: raise IOError('Not a valid input for "features" parameter!') if not isinstance(feature_set, dict) or not 'features' in feature_set: raise IOError('Not a valid input for "features" parameter!') # make new shapefile fields = [Field(f) for f in feature_set['fields']] if not outSR: sr_dict = feature_set['spatialReference'] if 'latestWkid' in sr_dict: outSR = int(sr_dict['latestWkid']) else: outSR = int(sr_dict['wkid']) g_type = feature_set['geometryType'] # add all fields w = shp_helper.shp(G_DICT[g_type].upper(), out_fc) field_map = [] for fld in fields: if fld.type not in [OID, SHAPE] + SKIP_FIELDS.keys(): if not any(['shape_' in fld.name.lower(), 'shape.' in fld.name.lower(), '(shape)' in fld.name.lower(), 'objectid' in fld.name.lower(), fld.name.lower() == 'fid']): field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) # search cursor to write rows s_fields = [fl for fl in fields if fl.name in [f[0] for f in field_map]] for feat in feature_set['features']: row = Row(feat, s_fields, outSR, g_type).values w.add_row(row[-1], [v if v else ' ' for v in row[:-1]]) w.save() print('Created: "{0}"'.format(out_fc)) # write projection file project(out_fc, outSR) return out_fc
def exportReplica(replica, out_folder): """converts a restapi.Replica() to a Shapefiles replica -- input restapi.Replica() object, must be generated from restapi.FeatureService.createReplica() out_folder -- full path to folder location where new files will be stored. """ if not hasattr(replica, 'replicaName'): print('Not a valid input! Must be generated from restapi.FeatureService.createReplica() method!') return # attachment directory and gdb set up att_loc = os.path.join(out_folder, 'Attachments') if not os.path.exists(att_loc): os.makedirs(att_loc) # set schema and create feature classes for layer in replica.layers: # download attachments att_dict = {} for attInfo in layer.attachments: out_file = assignUniqueName(os.path.join(att_loc, attInfo['name'])) with open(out_file, 'wb') as f: f.write(urllib.urlopen(attInfo['url']).read()) att_dict[attInfo['parentGlobalId']] = out_file.strip() if layer.features: # make new feature class sr = layer.spatialReference out_fc = validate_name(os.path.join(out_folder, layer.name + '.shp')) g_type = G_DICT[layer.geometryType] # add all fields layer_fields = [f for f in layer.fields if f.type not in (SHAPE, OID)] w = shp_helper.shp(g_type, out_fc) guid = None field_map = [] for fld in layer_fields: field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] if fld.type == 'esriFieldTypeGlobalID': guid = fld.name field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) w.add_field('ATTCH_PATH', 'C', '254') # search cursor to write rows s_fields = [f[0] for f in field_map] date_indices = [i for i,f in enumerate(layer_fields) if f.type == 'esriFieldTypeDate'] for feature in layer.features: row = [feature['attributes'][f] for f in s_fields] if guid: row += [att_dict[feature['attributes'][guid]]] for i in date_indices: row[i] = mil_to_date(row[i]) g_type = G_DICT[layer.geometryType] if g_type == 'Polygon': geom = feature['geometry']['rings'] elif g_type == 'Polyline': geom = feature['geometry']['paths'] elif g_type == 'Point': geom = [feature['geometry']['x'], feature['geometry']['y']] else: # multipoint - to do pass w.add_row(geom, [v if v else ' ' for v in row]) w.save() print('Created: "{0}"'.format(out_fc)) # write projection file project(out_fc, sr) return out_folder
def exportReplica(replica, out_folder): """converts a restapi.Replica() to a Shapefiles replica -- input restapi.Replica() object, must be generated from restapi.FeatureService.createReplica() out_folder -- full path to folder location where new files will be stored. """ if not hasattr(replica, "replicaName"): print "Not a valid input! Must be generated from restapi.FeatureService.createReplica() method!" return # attachment directory and gdb set up att_loc = os.path.join(out_folder, "Attachments") if not os.path.exists(att_loc): os.makedirs(att_loc) # set schema and create feature classes for layer in replica.layers: # download attachments att_dict = {} for attInfo in layer.attachments: out_file = os.path.join(att_loc, attInfo["name"]) with open(out_file, "wb") as f: f.write(urllib.urlopen(attInfo["url"]).read()) att_dict[attInfo["parentGlobalId"]] = out_file.strip() if layer.features: # make new feature class sr = layer.spatialReference out_fc = validate_name(os.path.join(out_folder, layer.name + ".shp")) g_type = G_DICT[layer.geometryType] # add all fields layer_fields = [f for f in layer.fields if f.type not in (SHAPE, OID)] w = shp_helper.shp(g_type, out_fc) guid = None field_map = [] for fld in layer_fields: field_name = fld.name.split(".")[-1][:10] field_type = SHP_FTYPES[fld.type] if fld.type == "esriFieldTypeGlobalID": guid = fld.name field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) w.add_field("ATTCH_PATH", "C", "254") # search cursor to write rows s_fields = [f[0] for f in field_map] date_indices = [i for i, f in enumerate(layer_fields) if f.type == "esriFieldTypeDate"] for feature in layer.features: row = [feature["attributes"][f] for f in s_fields] if guid: row += [att_dict[feature["attributes"][guid]]] for i in date_indices: row[i] = mil_to_date(row[i]) g_type = G_DICT[layer.geometryType] if g_type == "Polygon": geom = feature["geometry"]["rings"] elif g_type == "Polyline": geom = feature["geometry"]["paths"] elif g_type == "Point": geom = [feature["geometry"]["x"], feature["geometry"]["y"]] else: # multipoint - to do pass w.add_row(geom, row) w.save() print 'Created: "{0}"'.format(out_fc) # write projection file project(out_fc, sr) return out_folder
def layer_to_fc(self, out_fc, fields='*', where='1=1', records=None, params={}, get_all=False, sr=None): """Method to export a feature class from a service layer Required: out_fc -- full path to output feature class Optional: sr -- output spatial refrence (WKID) where -- optional where clause params -- dictionary of parameters for query flds -- list of fields for fc. If none specified, all fields are returned. Supports fields in list [] or comma separated string "field1,field2,.." records -- number of records to return. Default is none, will return maxRecordCount get_all -- option to get all records. If true, will recursively query REST endpoint until all records have been gathered. Default is False. """ if self.type == 'Feature Layer': if not fields: fields = '*' if fields == '*': _fields = self.fields else: if isinstance(fields, basestring): fields = fields.split(',') _fields = [f for f in self.fields if f.name in fields] # make new feature class if not sr: sr = self.spatialReference g_type = G_DICT[self.geometryType] # add all fields w = shp_helper.shp(g_type, out_fc) field_map = [] for fld in _fields: if fld.type not in [OID, SHAPE] + SKIP_FIELDS.keys(): if not any([ 'shape_' in fld.name.lower(), 'shape.' in fld.name.lower(), '(shape)' in fld.name.lower(), 'objectid' in fld.name.lower(), fld.name.lower() == 'fid' ]): field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) # search cursor to write rows s_fields = [f[0] for f in field_map] if not self.SHAPE.name in s_fields and 'SHAPE@' not in s_fields: s_fields.append('SHAPE@') query_resp = self.cursor(s_fields, where, records, params, get_all).response return exportFeatureSet(out_fc, query_resp, outSR=sr) else: print( 'Cannot convert layer: "{0}" to Feature Layer, Not a vector layer!' .format(self.name))
def exportFeatureSet(out_fc, feature_set, outSR=None): """export features (JSON result) to shapefile or feature class Required: out_fc -- output feature class or shapefile feature_set -- JSON response (feature set) obtained from a query Optional: outSR -- optional output spatial reference. If none set, will default to SR of result_query feature set. """ # validate features input (should be list or dict, preferably list) if isinstance(feature_set, basestring): try: feature_set = json.loads(feature_set) except: raise IOError('Not a valid input for "features" parameter!') if not isinstance(feature_set, dict) or not 'features' in feature_set: raise IOError('Not a valid input for "features" parameter!') # make new shapefile fields = [Field(f) for f in feature_set['fields']] if not outSR: sr_dict = feature_set['spatialReference'] if 'latestWkid' in sr_dict: outSR = int(sr_dict['latestWkid']) else: outSR = int(sr_dict['wkid']) g_type = feature_set['geometryType'] # add all fields w = shp_helper.shp(G_DICT[g_type].upper(), out_fc) field_map = [] for fld in fields: if fld.type not in [OID, SHAPE] + SKIP_FIELDS.keys(): if not any([ 'shape_' in fld.name.lower(), 'shape.' in fld.name.lower(), '(shape)' in fld.name.lower(), 'objectid' in fld.name.lower(), fld.name.lower() == 'fid' ]): field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) # search cursor to write rows s_fields = [fl for fl in fields if fl.name in [f[0] for f in field_map]] for feat in feature_set['features']: row = Row(feat, s_fields, outSR, g_type).values w.add_row(row[-1], [v if v else ' ' for v in row[:-1]]) w.save() print('Created: "{0}"'.format(out_fc)) # write projection file project(out_fc, outSR) return out_fc
def exportReplica(replica, out_folder): """converts a restapi.Replica() to a Shapefiles replica -- input restapi.Replica() object, must be generated from restapi.FeatureService.createReplica() out_folder -- full path to folder location where new files will be stored. """ if not hasattr(replica, 'replicaName'): print( 'Not a valid input! Must be generated from restapi.FeatureService.createReplica() method!' ) return # attachment directory and gdb set up att_loc = os.path.join(out_folder, 'Attachments') if not os.path.exists(att_loc): os.makedirs(att_loc) # set schema and create feature classes for layer in replica.layers: # download attachments att_dict = {} for attInfo in layer.attachments: out_file = assignUniqueName(os.path.join(att_loc, attInfo['name'])) with open(out_file, 'wb') as f: f.write(urllib.urlopen(attInfo['url']).read()) att_dict[attInfo['parentGlobalId']] = out_file.strip() if layer.features: # make new feature class sr = layer.spatialReference out_fc = validate_name( os.path.join(out_folder, layer.name + '.shp')) g_type = G_DICT[layer.geometryType] # add all fields layer_fields = [ f for f in layer.fields if f.type not in (SHAPE, OID) ] w = shp_helper.shp(g_type, out_fc) guid = None field_map = [] for fld in layer_fields: field_name = fld.name.split('.')[-1][:10] field_type = SHP_FTYPES[fld.type] if fld.type == 'esriFieldTypeGlobalID': guid = fld.name field_length = str(fld.length) if fld.length else "50" w.add_field(field_name, field_type, field_length) field_map.append((fld.name, field_name)) w.add_field('ATTCH_PATH', 'C', '254') # search cursor to write rows s_fields = [f[0] for f in field_map] date_indices = [ i for i, f in enumerate(layer_fields) if f.type == 'esriFieldTypeDate' ] for feature in layer.features: row = [feature['attributes'][f] for f in s_fields] if guid: row += [att_dict[feature['attributes'][guid]]] for i in date_indices: row[i] = mil_to_date(row[i]) g_type = G_DICT[layer.geometryType] if g_type == 'Polygon': geom = feature['geometry']['rings'] elif g_type == 'Polyline': geom = feature['geometry']['paths'] elif g_type == 'Point': geom = [feature['geometry']['x'], feature['geometry']['y']] else: # multipoint - to do pass w.add_row(geom, [v if v else ' ' for v in row]) w.save() print('Created: "{0}"'.format(out_fc)) # write projection file project(out_fc, sr) return out_folder