def _get_sdf(self, path_directory): """ Load the photos into a spatial data frame. :param path_directory: Path to directory containing photos. :return: SpatialDataFrame """ # load up the photos as a list of dictionaries photo_list = [ Photo(file).dictionary for file in os.listdir(path_directory) if file.lower().endswith('.jpg') ] # convert this list of dictionaries into a SpatialDataFrame photo_sdf = SpatialDataFrame(photo_list) # set the geometry field property photo_sdf.set_geometry('SHAPE', inplace=True, sr=SpatialReference(wkid=4326)) # reindex all the data and return the result return photo_sdf.reset_index(drop=True)
def from_featureclass(filename, **kwargs): """ Returns a GeoDataFrame from a feature class. Inputs: filename: full path to the feature class Optional Parameters: sql_clause: sql clause to parse data down where_clause: where statement sr: spatial reference object fields: list of fields to extract from the table """ from .. import SpatialDataFrame from arcgis.geometry import _types if HASARCPY: sql_clause = kwargs.pop('sql_clause', (None,None)) where_clause = kwargs.pop('where_clause', None) sr = kwargs.pop('sr', arcpy.Describe(filename).spatialReference or arcpy.SpatialReference(4326)) fields = kwargs.pop('fields', None) desc = arcpy.Describe(filename) if not fields: fields = [field.name for field in arcpy.ListFields(filename) \ if field.type not in ['Geometry']] if hasattr(desc, 'areaFieldName'): afn = desc.areaFieldName if afn in fields: fields.remove(afn) if hasattr(desc, 'lengthFieldName'): lfn = desc.lengthFieldName if lfn in fields: fields.remove(lfn) geom_fields = fields + ['SHAPE@'] flds = fields + ['SHAPE'] vals = [] geoms = [] geom_idx = flds.index('SHAPE') shape_type = desc.shapeType default_polygon = _types.Geometry(arcpy.Polygon(arcpy.Array([arcpy.Point(0,0)]* 3))) default_polyline = _types.Geometry(arcpy.Polyline(arcpy.Array([arcpy.Point(0,0)]* 2))) default_point = _types.Geometry(arcpy.PointGeometry(arcpy.Point())) default_multipoint = _types.Geometry(arcpy.Multipoint(arcpy.Array([arcpy.Point()]))) with arcpy.da.SearchCursor(filename, field_names=geom_fields, where_clause=where_clause, sql_clause=sql_clause, spatial_reference=sr) as rows: for row in rows: row = list(row) # Prevent curves/arcs if row[geom_idx] is None: row.pop(geom_idx) g = {} elif row[geom_idx].type in ['polyline', 'polygon']: g = _types.Geometry(row.pop(geom_idx).generalize(0)) else: g = _types.Geometry(row.pop(geom_idx)) if g == {}: if shape_type.lower() == 'point': g = default_point elif shape_type.lower() == 'polygon': g = default_polygon elif shape_type.lower() == 'polyline': g = default_point elif shape_type.lower() == 'multipoint': g = default_multipoint geoms.append(g) vals.append(row) del row del rows df = pd.DataFrame(data=vals, columns=fields) sdf = SpatialDataFrame(data=df, geometry=geoms) sdf.reset_index(drop=True, inplace=True) del df if sdf.sr is None: if sr is not None: sdf.sr = sr else: sdf.sr = sdf.geometry[sdf.geometry.first_valid_index()].spatialReference return sdf elif HASARCPY == False and \ HASPYSHP == True and\ filename.lower().find('.shp') > -1: geoms = [] records = [] reader = shapefile.Reader(filename) fields = [field[0] for field in reader.fields if field[0] != 'DeletionFlag'] for r in reader.shapeRecords(): atr = dict(zip(fields, r.record)) g = r.shape.__geo_interface__ g = _geojson_to_esrijson(g) geom = _types.Geometry(g) atr['SHAPE'] = geom records.append(atr) del atr del r, g del geom sdf = SpatialDataFrame(records) sdf.set_geometry(col='SHAPE') sdf.reset_index(inplace=True) return sdf elif HASARCPY == False and \ HASPYSHP == False and \ HASFIONA == True and \ (filename.lower().find('.shp') > -1 or \ os.path.dirname(filename).lower().find('.gdb') > -1): is_gdb = os.path.dirname(filename).lower().find('.gdb') > -1 if is_gdb: with fiona.drivers(): from arcgis.geometry import _types fp = os.path.dirname(filename) fn = os.path.basename(filename) geoms = [] atts = [] with fiona.open(path=fp, layer=fn) as source: meta = source.meta cols = list(source.schema['properties'].keys()) for idx, row in source.items(): geoms.append(_types.Geometry(row['geometry'])) atts.append(list(row['properties'].values())) del idx, row df = pd.DataFrame(data=atts, columns=cols) return SpatialDataFrame(data=df, geometry=geoms) else: with fiona.drivers(): from arcgis.geometry import _types geoms = [] atts = [] with fiona.open(path=filename) as source: meta = source.meta cols = list(source.schema['properties'].keys()) for idx, row in source.items(): geoms.append(_types.Geometry(row['geometry'])) atts.append(list(row['properties'].values())) del idx, row df = pd.DataFrame(data=atts, columns=cols) return SpatialDataFrame(data=df, geometry=geoms) return