123 votes
Accepted

Converting Pandas DataFrame to GeoDataFrame

Convert the DataFrame's content (e.g. Lat and Lon columns) into appropriate Shapely geometries first and then use them together with the original DataFrame to create a GeoDataFrame. from geopandas ...
user avatar
56 votes
Accepted

Joining (concat) list of similar dataframes in geopandas?

it seems that this is the right way to do that right now: rdf = gpd.GeoDataFrame( pd.concat( dataframesList, ignore_index=True) )
user avatar
  • 1,614
47 votes

Converting Pandas DataFrame to GeoDataFrame

Update 201912: The official documentation at https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html does it succinctly using geopandas.points_from_xy like so: gdf = ...
user avatar
  • 1,493
47 votes

Finding nearest point in other GeoDataFrame using GeoPandas

If you have large dataframes, I've found that scipy's cKDTree spatial index .query method returns very fast results for nearest neighbor searches. As it uses a spatial index it's orders of magnitude ...
user avatar
  • 571
41 votes

Filter by bounding box in GeoPandas

You can use the cx method on a geodataframe to select rows within a bounding box. For your example frames: xmin, ymin, xmax, ymax = df_sussex.total_bounds sac_sussex = df_sac.cx[xmin:xmax, ymin:ymax] ...
user avatar
  • 812
40 votes

Adding GeoPandas Dataframe to PostGIS table?

Using Panda's to_sql method and SQLAlchemy you can store a dataframe in Postgres. And since you're storing a Geodataframe, GeoAlchemy will handle the geom column for you. Here's a code sample: # ...
user avatar
39 votes
Accepted

How do I write a GeoPandas dataframe into a single file (preferably JSON or GeoPackage)?

To write to GeoJSON: dataframe.to_file("output.json", driver="GeoJSON") To write to GeoPackage: dataframe.to_file("output.gpkg", driver="GPKG") Documentation is here, though somewhat sparse.
user avatar
  • 6,738
38 votes
Accepted

Geopandas buffer using geodataframe while maintaining the dataframe

from shapely.geometry import Point import pandas as pd import geopandas as gpd p1 = Point((1,2)) p2 = Point((5,6)) df = pd.DataFrame({'a': [11,22]}) gdf = gpd.GeoDataFrame(df, geometry = [p1,p2]) gdf #...
user avatar
  • 626
36 votes

Finding all neighbors using GeoPandas

The following script finds and adds neighbors as a new field value joined by comma. import geopandas as gpd file = "C:/path/to/shapefile.shp" # open file gdf = gpd.read_file(file) # ...
user avatar
35 votes
Accepted

Cannot get plot() in geopandas to produce a map of the GeoDataFrame

The examples provided are for executing the scripts in a Jupyter/IPython notebooks environment. In a normal Python environment, you need to import matplotlib to show the image import geopandas as gpd ...
user avatar
  • 51.2k
34 votes
Accepted

Rasterize a shapefile with Geopandas or fiona - python

You are on the right track and the geopandas GeoDataFrame is a good choice for rasterization over Fiona. Fiona is a great toolset, but I think that the DataFrame is better suited to shapefiles and ...
user avatar
33 votes

Creating polygon grid using GeoPandas

Previous answer gives correct result, but I allowed myself to improve the code, to avoid many unnecessary lines, as well as iterating on indexes instead of values on the list. import geopandas as gpd ...
user avatar
  • 569
32 votes
Accepted

Listing all polygon vertices coordinates using GeoPandas

Not sure if one line method exists, but the following ways could work. (Solutions are for the first feature's geometry, and they are just for Polygon, not for MultiPolygon) Solution 1: boundary ...
user avatar
31 votes
Accepted

Finding nearest point in other GeoDataFrame using GeoPandas

You can directly use the Shapely function Nearest points (the geometries of the GeoSeries are Shapely geometries): from shapely.ops import nearest_points # unary union of the gpd2 geomtries pts3 = ...
user avatar
  • 51.2k
30 votes

Returning lat and long of centroid point with GeoPandas

Ran into this problem myself. If you want the x and y as separate GeoDataFrame columns, then this works nicely: gdf["x"] = gdf.centroid.map(lambda p: p.x) gdf["y"] = gdf.centroid.map(lambda p: p.y) ...
user avatar
30 votes
Accepted

Getting polygon areas using GeoPandas

If the crs of the GeoDataFrame is known (EPSG:4326 unit=degree, here), you don't need Shapely, nor pyproj in your script because GeoPandas uses them). import geopandas as gpd test = gpd.read_file("...
user avatar
  • 51.2k
26 votes

Joining (concat) list of similar dataframes in geopandas?

I just experimented with this - maybe in GeoPandas 0.2.1 and Pandas 0.20.3 it is a bit more concise: gdf = pd.concat([gdf1, gdf2]) gdf is automatically created as a GeoDataFrame. Of course if there ...
user avatar
  • 1,604
25 votes
Accepted

Creating polygon grid using GeoPandas

There are many solutions. One of them import geopandas as gpd from shapely.geometry import Polygon import numpy as np points = gpd.read_file('points.shp') xmin,ymin,xmax,ymax = points....
user avatar
  • 51.2k
24 votes
Accepted

Dissolving polygons based on attributes with Python (shapely, fiona)?

The question is about Fiona and Shapely and the other answer using GeoPandas requires to also know Pandas. Moreover GeoPandas uses Fiona to read/write shapefiles. I do not question here the utility ...
user avatar
  • 51.2k
23 votes
Accepted

Reading the names of geodatabase file layers in Python

The fiona.listlayers() function returns a list of names of layers in a dataset. import fiona fiona.listlayers('NYCFutureHighTideWithSLR.gdb') Any of the elements of the list can be used as a value ...
user avatar
  • 8,538
21 votes
Accepted

What units are used by GeoPandas/Shapely area and distance functions?

Shapely uses a cartesian plane system for computing geometries (distance = euclidean distance) Shapely does not support coordinate system transformations. All operations on two or more features ...
user avatar
  • 51.2k
21 votes
Accepted

geopandas spatial join extremely slow

adding the argument predicate='within' in the sjoin function speeds up the point-in-polygon operation dramatically. Default value is predicate='intersects', which I guess would also lead to correct ...
user avatar
21 votes
Accepted

Reading raw data into geopandas

You can pass the json directly to the GeoDataFrame constructor: import geopandas as gpd import requests data = requests.get("https://data.cityofnewyork.us/api/geospatial/arq3-7z49?method=export&...
user avatar
  • 52.8k
21 votes
Accepted

Looking for GeoPandas QGIS equivalent of ArcGIS "Select by location" tool

If poly is a GeoDataFrame with a single geometry, extract this: polygon = poly.geometry[0] Then, you can use the within method to check which points are within the polygon: points.within(polygon) ...
user avatar
  • 3,418
21 votes
Accepted

How can I superimpose a geopandas dataframe on a raster plot?

For me it works if I pass the matplotlib ax object explicitly to rasterio.plot.show: fig, ax = plt.subplots(figsize=(15, 15)) rasterio.plot.show(raster, ax=ax) countries.plot(ax=ax, facecolor='none', ...
user avatar
  • 3,418
21 votes
Accepted

Dissolve not based on attribute in GeoPandas?

You will lose all attributes but unary_union will return one big multipolygon which you can turn into singleparts with .geoms: import geopandas as gpd df = gpd.read_file(r"C:\Test\New_Shapefile.shp") ...
user avatar
  • 52.4k
19 votes
Accepted

Turning GeoDataFrame of x,y coordinates into Linestrings using GROUPBY?

I think I found an interim solution, which I'm posting in case it's useful for anyone: import pandas as pd import numpy as np from geopandas import GeoDataFrame from shapely.geometry import Point, ...
user avatar
  • 1,329
19 votes
Accepted

Merging overlapping features using Geopandas?

The GeoDataFrame import geopandas as gpd g1 = gpd.GeoDataFrame.from_file("poly_intersect.shp") g1.shape (4, 3) 1) You can use the itertools module a) If you want to merge the intersections of the ...
user avatar
  • 51.2k
18 votes

Finding all neighbors using GeoPandas

This is an addendum to @Kadir's answer (which works great). For one, instead of using not disjoint you can just use touches directly, which does the same thing but is easier to read. If, as the OP ...
user avatar
18 votes
Accepted

GeoPandas spatial join and count

Try: import geopandas as gpd import pandas as pd polys = gpd.read_file(r"C:\Test\Shapes.shp") points = gpd.read_file(r"C:\Test\POI.shp") dfsjoin = gpd.sjoin(polys, points) #...
user avatar
  • 52.4k

Only top scored, non community-wiki answers of a minimum length are eligible