Python geopandas.GeoDataFrame() Examples
The following are 30
code examples of geopandas.GeoDataFrame().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
geopandas
, or try the search function
.
Example #1
Source File: __init__.py From dinosar with MIT License | 8 votes |
def load_inventory(inventoryJSON): """Load inventory saved with asf.archive.save_inventory(). Parameters ---------- inventoryJSON : str dinsar inventory file (query.geojson) Returns ------- gf : GeoDataFrame A geopandas GeoDataFrame """ gf = gpd.read_file(inventoryJSON) gf["timeStamp"] = pd.to_datetime(gf.sceneDate, format="%Y-%m-%d %H:%M:%S") gf["sceneDateString"] = gf.timeStamp.apply(lambda x: x.strftime("%Y-%m-%d")) gf["dateStamp"] = pd.to_datetime(gf.sceneDateString) gf["utc"] = gf.timeStamp.apply(lambda x: x.strftime("%H:%M:%S")) gf["relativeOrbit"] = gf.relativeOrbit.astype("int") gf.sort_values("relativeOrbit", inplace=True) gf["orbitCode"] = gf.relativeOrbit.astype("category").cat.codes return gf
Example #2
Source File: LSDMap_HillslopeMorphology.py From LSDMappingTools with MIT License | 7 votes |
def WriteHillslopeTracesShp(DataDirectory,FilenamePrefix,ThinningFactor=1, CustomExtent=[-9999]): """ This function writes a shapefile of hillslope traces Args: DataDirectory: the data directory FilenamePrefix: the file name prefix ThinningFactor (int): This determines how many of the traces are discarded. Higher numbers mean more traces are discarded CustomExtent (list): if this is [-9999] the extent is grabbed from the raster. Otherwise you give it a 4 element list giving the extent of the area of interest. Returns: None, but writes a shapefile Author: MDH """ #read the raw data to geodataframe geo_df = ReadHillslopeTraces(DataDirectory,FilenamePrefix,ThinningFactor, CustomExtent) Suffix = '_hillslope_traces' WriteFilename = DataDirectory+FilenamePrefix+Suffix+'.shp' #aggregate these points with group by geo_df = geo_df.groupby(['HilltopID'])['geometry'].apply(lambda x: LineString(x.tolist())) geo_df = GeoDataFrame(geo_df, geometry='geometry') geo_df.to_file(WriteFilename, driver='ESRI Shapefile')
Example #3
Source File: test_utils.py From momepy with MIT License | 7 votes |
def test_network_false_nodes(self): test_file_path2 = mm.datasets.get_path("tests") self.false_network = gpd.read_file(test_file_path2, layer="network") fixed = mm.network_false_nodes(self.false_network) assert len(fixed) == 55 assert isinstance(fixed, gpd.GeoDataFrame) assert self.false_network.crs.equals(fixed.crs) fixed_series = mm.network_false_nodes(self.false_network.geometry) assert len(fixed_series) == 55 assert isinstance(fixed_series, gpd.GeoSeries) assert self.false_network.crs.equals(fixed_series.crs) with pytest.raises(TypeError): mm.network_false_nodes(list()) multiindex = self.false_network.explode() fixed_multiindex = mm.network_false_nodes(multiindex) assert len(fixed_multiindex) == 55 assert isinstance(fixed, gpd.GeoDataFrame)
Example #4
Source File: __init__.py From CityEnergyAnalyst with MIT License | 6 votes |
def edges_df(self): """ This property is expected to return a GeoDataFrame containing the edges of the network to display including the data. Any columns included will be shown in the Tooltip of the map, except for those starting with an underscore. There are some special columns that must be added here, that are used for visualization purposes: - _LineWidth: The line width to use for the edges. This can be a computed property based on edge data :return: :rtype: geopandas.GeoDataFrame """ raise NotImplementedError("Please implement edges_df in subclass")
Example #5
Source File: excel_to_shapefile.py From CityEnergyAnalyst with MIT License | 6 votes |
def excel_to_shapefile(excel_file, shapefile, index, crs, polygon=True): """ Expects the Excel file to be in the format created by ``cea shapefile-to-excel`` :param polygon: Set this to ``False`` if the Excel file contains polyline data in the ``geometry`` column instead of the default polygon data. (polylines are used for representing streets etc.) :type polygon: bool """ df = pd.read_excel(excel_file) if polygon: geometry = [shapely.geometry.polygon.Polygon(json.loads(g)) for g in df.geometry] else: geometry = [shapely.geometry.LineString(json.loads(g)) for g in df.geometry] df.drop('geometry', axis=1) gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry) gdf.to_file(shapefile, driver='ESRI Shapefile', encoding='ISO-8859-1')
Example #6
Source File: process_results.py From CityEnergyAnalyst with MIT License | 6 votes |
def write_coordinates_to_shp_file(config, locator, list_geotranch, name): """ Write grid.shp and thermal_network.shp on base of list of coordinate data :param list_geotranch: tuples with geo data of startnode and endnode :type list_geotranch: list(float, float) :param name: filename of shp file :type name: string :return: shp file stored in \\inputs\\networks\\ :rtype: Nonetype """ input_street_shp = locator.get_street_network() output_path_shp = locator.get_electric_network_output_location(name) geometry = [shapely.geometry.LineString(json.loads(g)) for g in list_geotranch] gdf_street = gpd.GeoDataFrame.from_file(input_street_shp) lat, lon = get_lat_lon_projected_shapefile(gdf_street) crs = get_projected_coordinate_system(lat, lon) gdf = gpd.GeoDataFrame(crs=crs, geometry=geometry) gdf.to_file(output_path_shp, driver='ESRI Shapefile', encoding='ISO-8859-1')
Example #7
Source File: overpass.py From urbansprawl with MIT License | 6 votes |
def buildings_from_point(date, point, distance, retain_invalid=False): """ Get building footprints within some distance north, south, east, and west of a lat-long point. Parameters ---------- date : string query the database at a certain timestamp point : tuple a lat-long point distance : numeric distance in meters retain_invalid : bool if False discard any building footprints with an invalid geometry Returns ------- GeoDataFrame """ bbox = ox.bbox_from_point(point=point, distance=distance) north, south, east, west = bbox return create_buildings_gdf(date=date, north=north, south=south, east=east, west=west, retain_invalid=retain_invalid)
Example #8
Source File: overpass.py From urbansprawl with MIT License | 6 votes |
def buildings_from_address(date, address, distance, retain_invalid=False): """ Get building footprints within some distance north, south, east, and west of an address. Parameters ---------- date : string query the database at a certain timestamp address : string the address to geocode to a lat-long point distance : numeric distance in meters retain_invalid : bool if False discard any building footprints with an invalid geometry Returns ------- GeoDataFrame """ # geocode the address string to a (lat, lon) point point = ox.geocode(query=address) # get buildings within distance of this point return buildings_from_point(date, point, distance, retain_invalid=retain_invalid)
Example #9
Source File: overpass.py From urbansprawl with MIT License | 6 votes |
def buildings_from_polygon(date, polygon, retain_invalid=False): """ Get building footprints within some polygon. Parameters ---------- date : string query the database at a certain timestamp polygon : Polygon retain_invalid : bool if False discard any building footprints with an invalid geometry Returns ------- GeoDataFrame """ return create_buildings_gdf(date=date, polygon=polygon, retain_invalid=retain_invalid)
Example #10
Source File: overpass.py From urbansprawl with MIT License | 6 votes |
def buildings_from_place(date, place, which_result=1, retain_invalid=False): """ Get building footprints within the boundaries of some place. Parameters ---------- date : string query the database at a certain timestamp place : string the query to geocode to get geojson boundary polygon which_result : int result number to retrieve from geocode/download when using query string retain_invalid : bool if False discard any building footprints with an invalid geometry Returns ------- GeoDataFrame """ city = ox.gdf_from_place(place, which_result=which_result) polygon = city['geometry'].iloc[0] return create_buildings_gdf(date=date, polygon=polygon, retain_invalid=retain_invalid) ####################################################################### ### Street network graph #######################################################################
Example #11
Source File: sentinel.py From sentinelsat with GNU General Public License v3.0 | 6 votes |
def to_geodataframe(products): """Return the products from a query response as a GeoPandas GeoDataFrame with the values in their appropriate Python types. """ try: import geopandas as gpd import shapely.wkt except ImportError: raise ImportError( "to_geodataframe requires the optional dependencies GeoPandas and Shapely." ) crs = "EPSG:4326" # WGS84 if len(products) == 0: return gpd.GeoDataFrame(crs=crs, geometry=[]) df = SentinelAPI.to_dataframe(products) geometry = [shapely.wkt.loads(fp) for fp in df["footprint"]] # remove useless columns df.drop(["footprint", "gmlfootprint"], axis=1, inplace=True) return gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
Example #12
Source File: utils.py From momepy with MIT License | 6 votes |
def _lines_to_gdf(net, lines, points, nodeID): """ Generate linestring gdf from edges. Helper for nx_to_gdf. """ starts, ends, edge_data = zip(*net.edges(data=True)) if lines is True: node_start = [] node_end = [] for s in starts: node_start.append(net.nodes[s][nodeID]) for e in ends: node_end.append(net.nodes[e][nodeID]) gdf_edges = gpd.GeoDataFrame(list(edge_data)) if points is True: gdf_edges["node_start"] = node_start gdf_edges["node_end"] = node_end if "crs" in net.graph.keys(): gdf_edges.crs = net.graph["crs"] return gdf_edges
Example #13
Source File: plot_hillslope_morphology.py From LSDMappingTools with MIT License | 6 votes |
def WriteHillslopeTracesShp(DataDirectory,FilenamePrefix): """ This function writes a shapefile of hillslope traces Args: DataDirectory: the data directory FilenamePrefix: the file name prefix Author: MDH """ #read the raw data to geodataframe geo_df = ReadHillslopeTraces(DataDirectory,FilenamePrefix) Suffix = '_hillslope_traces' WriteFilename = DataDirectory+FilenamePrefix+Suffix+'.shp' #aggregate these points with group by geo_df = geo_df.groupby(['HilltopID'])['geometry'].apply(lambda x: LineString(x.tolist())) geo_df = GeoDataFrame(geo_df, geometry='geometry') geo_df.to_file(WriteFilename, driver='ESRI Shapefile')
Example #14
Source File: utils.py From momepy with MIT License | 6 votes |
def gdf_to_nx(gdf_network, approach="primal", length="mm_len"): """ Convert LineString GeoDataFrame to networkx.MultiGraph Parameters ---------- gdf_network : GeoDataFrame GeoDataFrame containing objects to convert approach : str, default 'primal' Decide wheter genereate ``'primal'`` or ``'dual'`` graph. length : str, default mm_len name of attribute of segment length (geographical) which will be saved to graph Returns ------- networkx.Graph Graph """ gdf_network = gdf_network.copy() if "key" in gdf_network.columns: gdf_network.rename(columns={"key": "__key"}, inplace=True) # generate graph from GeoDataFrame of LineStrings net = nx.MultiGraph() net.graph["crs"] = gdf_network.crs gdf_network[length] = gdf_network.geometry.length fields = list(gdf_network.columns) if approach == "primal": _generate_primal(net, gdf_network, fields) elif approach == "dual": _generate_dual(net, gdf_network, fields) else: raise ValueError( "Approach {} is not supported. Use 'primal' or 'dual'.".format(approach) ) return net
Example #15
Source File: __init__.py From CityEnergyAnalyst with MIT License | 6 votes |
def nodes_df(self): """ This property is expected to return a GeoDataFrame containing the nodes of the network to display including the data. Any columns included will be shown in the Tooltip of the map, except for those starting with an underscore. There are some special columns that must be added here, that are used for visualization purposes: - _Radius: The radius of the node. This can be a computed property based on node data. - _FillColor: The color ([R, G, B]) to use for the line, serialized as JSON. This can be a computed property based on node data. :return: :rtype: geopandas.GeoDataFrame """ raise NotImplementedError("Please implement nodes_df in subclass")
Example #16
Source File: utils.py From momepy with MIT License | 6 votes |
def unique_id(objects): """ Add an attribute with unique ID to each row of GeoDataFrame. Parameters ---------- objects : GeoDataFrame GeoDataFrame containing objects to analyse Returns ------- Series Series containing resulting values. """ series = range(len(objects)) return series
Example #17
Source File: s3.py From xcube with MIT License | 6 votes |
def write_data(self, data: Any, data_id: str = None, writer_id: str = None, replace: bool = False, **write_params) -> str: assert_instance(data, (xr.Dataset, MultiLevelDataset, gpd.GeoDataFrame)) if not writer_id: if isinstance(data, xr.Dataset): predicate = get_data_accessor_predicate(type_id=TYPE_ID_DATASET, format_id='zarr', storage_id=_STORAGE_ID) else: raise DataStoreError(f'Unsupported data type {type(data)}') extensions = find_data_writer_extensions(predicate=predicate) writer_id = extensions[0].name data_id = self._ensure_valid_data_id(data_id, data) path = self._resolve_data_id_to_path(data_id) self._new_s3_writer(writer_id).write_data(data, data_id=path, replace=replace, **write_params) self.register_data(data_id, data) return data_id
Example #18
Source File: __init__.py From dinosar with MIT License | 6 votes |
def save_geojson_footprints(gf): """Save all frames from each date as separate geojson file. JSON footprints with metadata are easily visualized if pushed to GitHub. This saves a bunch of [date].geojson files in local directory. Parameters ---------- gf : GeoDataFrame a pandas geodataframe from load_asf_json """ attributes = ("granuleName", "downloadUrl", "geometry") gb = gf.groupby(["relativeOrbit", "sceneDateString"]) S = gf.groupby("relativeOrbit").sceneDateString.unique() for orbit, dateList in S.iteritems(): os.makedirs(str(orbit)) for date in dateList: dftmp = gf.loc[gb.groups[(orbit, date)], attributes].reset_index(drop=True) outname = f"{orbit}/{date}.geojson" dftmp.to_file(outname, driver="GeoJSON")
Example #19
Source File: data_cleaner.py From data-cleaner with MIT License | 6 votes |
def simplificar_geometria(self, tolerance=0.5, keep_original=True, inplace=False): """Simplifica una geometría para que resulte en un objeto de menor tamaño y complejidad, que a la vez retenga sus características esenciales. Args: tolerance (float): Nivel de tolerancia en la transformación. Returns: pandas.Series: Serie de geometrías. """ if isinstance(self.df, gpd.GeoDataFrame): self.df.geometry = self.df.geometry.simplify(tolerance) return self.df.geometry else: raise TypeError('El dataframe no es de tipo GeoDataFrame.')
Example #20
Source File: annotators.py From EarthSim with BSD 3-Clause "New" or "Revised" License | 6 votes |
def poly_to_geopandas(polys, columns): """ Converts a GeoViews Paths or Polygons type to a geopandas dataframe. Parameters ---------- polys : gv.Path or gv.Polygons GeoViews element columns: list(str) List of columns Returns ------- gdf : Geopandas dataframe """ rows = [] for g in polys.geom(): rows.append(dict({c: '' for c in columns}, geometry=g)) return gpd.GeoDataFrame(rows, columns=columns+['geometry'])
Example #21
Source File: prepare.py From gridfinder with MIT License | 6 votes |
def clip_rasters(folder_in, folder_out, aoi_in, debug=False): """Read continental rasters one at a time, clip to AOI and save Parameters ---------- folder_in : str, Path Path to directory containing rasters. folder_out : str, Path Path to directory to save clipped rasters. aoi_in : str, Path Path to an AOI file (readable by Fiona) to use for clipping. """ if isinstance(aoi_in, gpd.GeoDataFrame): aoi = aoi_in else: aoi = gpd.read_file(aoi_in) coords = [json.loads(aoi.to_json())["features"][0]["geometry"]] for file_path in os.listdir(folder_in): if file_path.endswith(".tif"): if debug: print(f"Doing {file_path}") ntl_rd = rasterio.open(os.path.join(folder_in, file_path)) ntl, affine = mask(dataset=ntl_rd, shapes=coords, crop=True, nodata=0) if ntl.ndim == 3: ntl = ntl[0] save_raster(folder_out / file_path, ntl, affine)
Example #22
Source File: vectorxarray.py From geocube with BSD 3-Clause "New" or "Revised" License | 6 votes |
def to_geodataframe(self): """ Convert to a GeoDataFrame. Returns ------- :obj:`geopandas.GeoDataFrame` """ self._validate_operation() out_obj = self._obj.drop("crs") extra_coords = list(set(list(out_obj.coords)) - {"geometry"}) if extra_coords: out_obj = out_obj.copy().reset_coords(extra_coords) geodf = gpd.GeoDataFrame(out_obj.to_dataframe().reset_index()) geodf.crs = self._obj.coords["crs"].attrs["crs_wkt"] return geodf
Example #23
Source File: geobox.py From geocube with BSD 3-Clause "New" or "Revised" License | 6 votes |
def load_vector_data(vector_data): """ Parameters ---------- vector_data: str or :obj:`geopandas.GeoDataFrame` A file path to an OGR supported source or GeoDataFrame containing the vector data. Returns ------- :obj:`geopandas.GeoDataFrame` containing the vector data. """ logger = get_logger() if isinstance(vector_data, str): vector_data = geopandas.read_file(vector_data) elif not isinstance(vector_data, geopandas.GeoDataFrame): vector_data = geopandas.GeoDataFrame(vector_data) if vector_data.empty: raise VectorDataError("Empty GeoDataFrame.") if "geometry" not in vector_data.columns: raise VectorDataError( "'geometry' column missing. Columns in file: " f"{vector_data.columns.values.tolist()}" ) # make sure projection is set if not vector_data.crs: vector_data.crs = "EPSG:4326" logger.warning( "Projection not defined in `vector_data`." " Setting to geographic (EPSG:4326)." ) return vector_data
Example #24
Source File: alchemy.py From ibis with Apache License 2.0 | 6 votes |
def _maybe_to_geodataframe(df, schema): """ If the required libraries for geospatial support are installed, and if a geospatial column is present in the dataframe, convert it to a GeoDataFrame. """ def to_shapely(row, name): return shape.to_shape(row[name]) if row[name] is not None else None if len(df) and geospatial_supported: geom_col = None for name, dtype in schema.items(): if isinstance(dtype, dt.GeoSpatial): geom_col = geom_col or name df[name] = df.apply(lambda x: to_shapely(x, name), axis=1) if geom_col: df = geopandas.GeoDataFrame(df, geometry=geom_col) return df
Example #25
Source File: schemas.py From CityEnergyAnalyst with MIT License | 6 votes |
def write(self, df, *args, **kwargs): """ :type df: geopandas.GeoDataFrame """ from cea.utilities.standardize_coordinates import (get_lat_lon_projected_shapefile, get_projected_coordinate_system) self.validate(df) path_to_shp = self(*args, **kwargs) parent_folder = os.path.dirname(path_to_shp) if not os.path.exists(parent_folder): os.makedirs(parent_folder) lat, lon = get_lat_lon_projected_shapefile(df) # get coordinate system and re project to UTM df = df.to_crs(get_projected_coordinate_system(lat, lon)) df.to_file(path_to_shp)
Example #26
Source File: conftest.py From earthpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def basic_geometry_gdf(basic_geometry): """ A GeoDataFrame containing the basic geometry Returns ------- GeoDataFrame containing the basic_geometry polygon """ gdf = gpd.GeoDataFrame(geometry=[basic_geometry], crs="epsg:4326") return gdf
Example #27
Source File: data_cleaner.py From data-cleaner with MIT License | 5 votes |
def save(self, output_path, geometry_name='geojson', geometry_crs='epsg:4326'): """Guarda los datos en un nuevo CSV con formato estándar. El CSV se guarda codificado en UTF-8, separado con "," y usando '"' comillas dobles como caracter de enclosing.""" if isinstance(self.df, gpd.GeoDataFrame): # Convierte la proyección, si puede. if geometry_crs: try: self.df.crs = self.source_crs self.df = self.df.to_crs({'init': geometry_crs}) except Exception as e: print(e) print("Se procede sin re-proyectar las coordenadas.") if output_path.endswith('.csv'): self._set_json_geometry(geometry_name) # Guarda el archivo en formato GeoJSON o KML. if output_path.endswith('json'): # Acepta .json y .geojson. self.df.to_file(output_path, driver='GeoJSON') return elif output_path.endswith('kml'): self._save_to_kml(output_path) return self.df.set_index(self.df.columns[0]).to_csv( output_path, encoding=self.OUTPUT_ENCODING, sep=self.OUTPUT_SEPARATOR, quotechar=self.OUTPUT_QUOTECHAR)
Example #28
Source File: test_clip.py From earthpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def two_line_gdf(): """ Create Line Objects For Testing """ linea = LineString([(1, 1), (2, 2), (3, 2), (5, 3)]) lineb = LineString([(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)]) gdf = gpd.GeoDataFrame([1, 2], geometry=[linea, lineb], crs="epsg:4326") return gdf
Example #29
Source File: __init__.py From CityEnergyAnalyst with MIT License | 5 votes |
def _plot_div_producer(self): """ Since this plot doesn't use plotly to plot, we override _plot_div_producer to return a string containing the html div to use for this plot. The template ``network_plot.html`` expects some parameters: - hash: this is used to make the html id's in the plot unique - edges: a GeoJson serialization of the networks edges and data - nodes: a GeoJson serialization of the networks nodes and data, - colors: a JSON dictionary of [R, G, B] arrays for the colors to use - zone: a GeoJson serialization of the zone's buildings - district: a GeoJson serialization of the distrct's buildings :return: a str containing a full html ``<div/>`` that includes the js code to display the map. """ import os import hashlib import random from jinja2 import Template zone_df = geopandas.GeoDataFrame.from_file(self.locator.get_zone_geometry()).to_crs( get_geographic_coordinate_system()) zone_df["_FillColor"] = json.dumps(self.colors["zone"]) zone = zone_df.to_json(show_bbox=True) district_df = geopandas.GeoDataFrame.from_file(self.locator.get_surroundings_geometry()).to_crs( get_geographic_coordinate_system()) district_df["_FillColor"] = json.dumps(self.colors["district"]) district = district_df.to_json(show_bbox=True) edges = self.edges_df.to_json(show_bbox=True) nodes = self.nodes_df.to_json(show_bbox=True) hash = hashlib.md5(str(random.random()) + edges + nodes).hexdigest() template = os.path.join(os.path.dirname(__file__), "network_plot.html") div = Template(open(template).read()).render(hash=hash, edges=edges, nodes=nodes, zone=zone, district=district) return div
Example #30
Source File: test_clip.py From earthpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def multi_line(two_line_gdf): """ Create a multi-line GeoDataFrame. This has one multi line and another regular line. """ # Create a single and multi line object multiline_feat = two_line_gdf.unary_union linec = LineString([(2, 1), (3, 1), (4, 1), (5, 2)]) out_df = gpd.GeoDataFrame( geometry=gpd.GeoSeries([multiline_feat, linec]), crs="epsg:4326", ) out_df = out_df.rename(columns={0: "geometry"}).set_geometry("geometry") out_df["attr"] = ["road", "stream"] return out_df