joris
  • Member for 9 years, 5 months
  • Last seen this week
Looking for GeoPandas QGIS equivalent of ArcGIS "Select by location" tool
Accepted answer
21 votes

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) ...

View answer
How can I superimpose a geopandas dataframe on a raster plot?
Accepted answer
19 votes

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', ...

View answer
Colorizing polygons based on color values in dataframe column?
Accepted answer
16 votes

The column= keyword can be used if you have values in a column which need to be mapped to a color (with a certain color map). But if you already have actual color names that you want to use directly, ...

View answer
Shapely polygon union results in strange artifacts of tiny, non-overlapping areas
Accepted answer
14 votes

This is typically a result of the the borders not fitting perfectly one next to another (and this is very easy to get with floating point coordinates). As an example, I use the world dataset ...

View answer
Remove empty geometries from geodataframe
Accepted answer
12 votes

The GeoDataFrame and GeoSeries have an is_empty attribute, similar to shapely geometry objects. You can use this to filter out the empty ones: In [7]: lines = geopandas.GeoSeries([ ...: ...

View answer
Change marker size in plot with GeoPandas
12 votes

In geopandas >= 0.3 (released September 2017), the plotting of points is based on the scatter plot method of matplotlib under the hood, and this accepts a variable markersize. So now you can actually ...

View answer
Getting bounding box from Point type GeoDataFrame to getting data from Osmnx
Accepted answer
11 votes

You can use the total_bounds property for this. Small example: In [83]: from shapely.geometry import Point In [84]: import geopandas In [86]: import random In [87]: df = geopandas.GeoDataFrame({'...

View answer
How to calculate mean value of a raster for each polygon in a shapefile
Accepted answer
11 votes

You can use the rasterstats package for this. For example (assuming you have a geopandas.GeoDataFrame called gdf): from rasterstats import zonal_stats with rasterio.open("/path/to/raster.tif") as ...

View answer
geopandas dissolve overlapping polygons
11 votes

Using this example GeoSeries: s = geopandas.GeoSeries([Polygon([(0, 0), (0, 2), (2, 2), (2, 0)]), Polygon([(0, 1), (0, 3), (2, 3), (2, 1)]),Polygon([(1, 0), (1, 2), (3, 2), (3, 0)]), Polygon([(4, 4), ...

View answer
Constant future warnings with new pyproj
Accepted answer
10 votes

The +init=epsg:xxxx syntax (or in form of a {'init': 'epsg:xxx'}) is being deprecated in PROJ / pyproj (see here. You are indeed not using it directly, but, the previous releases of GeoPandas (<= 0....

View answer
How to fix 'GeoSeries' object has no attribute '_geom'
Accepted answer
9 votes

When you are at the following line of your script: print((row.geometry).within(buddies_buf.geometry)) then row.geometry is a single shapely Point (row is a Series representing a single row, so row....

View answer
Geometry Data in GeoDataFrame will not plot
7 votes

You need to ensure you have actual geometry objects in the dataframe, and not only a string representation of it (which is likely since it is imported from csv). You can convert the column with ...

View answer
Left join using merge in geopandas
Accepted answer
6 votes

When merging by attributes, you are actually using the pandas merge (or join) method or function (see its docstring: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html)....

View answer
How to process 2 GeoDataFrames by rows pairwise?
Accepted answer
6 votes

If the two GeoDataFrames are "aligned" (same length with same index), you can actually directly use the distance method: this accepts another GeoSeries or GeoDataFrame, and then the distance will be ...

View answer
Convert polygon bounding box to geodataframe?
Accepted answer
6 votes

You are almost there with what you tried, and using the box method is indeed the best way. With the list comprehension, you can do b = [box(l, b, r, t) for l, b, r, t in zip(df.left, df.bottom, df....

View answer
Merging two datasets where polygons are nearly identical using geopandas?
Accepted answer
6 votes

Consider the following example dataframes: import geopandas from shapely.geometry import Polygon df1 = geopandas.GeoDataFrame( {'geometry': [Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]), ...

View answer
Truncating points precision
Accepted answer
5 votes

The simplify method is meant for simplifying lines or polygons: reducing the number of vertices (thus reducing the number of coordinates, not the precision of each coordinate). So for points, this has ...

View answer
ValueError: Invalid field type <class 'shapely.geometry.polygon.Polygon'> when exporting shp?
Accepted answer
5 votes

Shapefiles have some limitations, including: They cannot have multiple columns with geometry data The geometries cannot contains multiple types (so eg both Points and LineStrings) In both cases you ...

View answer
In to_crs of GeoPandas, init proj4 string works, but new style doesn't
Accepted answer
4 votes

I reported this as a bug here: https://github.com/pyproj4/pyproj/issues/549 In the meantime, you can keep using the deprecated "{'init':'epsg:4326'}" syntax a bit longer (and ignore the warning for ...

View answer
Merge overlapping/neighboring polygons - GeoPandas
4 votes

You can first union all polygons with unary_union: single_multi_polygon = all_Rapa_Nui.unary_union This should now be a single MultiPolygon consisting of two polygons for the two islands. And then ...

View answer
Perform sjoin in geopandas leads to:'AttributeError: 'GeoSeries' object has no attribute 'columns''
4 votes

geopandas.sjoin expects a GeoDataFrame, not a GeoSeries. So instead of gpd.sjoin(gdf["geom"], exp_union_gdf , how="inner", op='intersects') you can do gpd.sjoin(gdf, exp_union_gdf , how="inner", ...

View answer
Performing sjoin on polygons and lines without intersection using GeoPandas?
Accepted answer
3 votes

The geopandas.sjoin function only supports the 'intersects', 'within' and 'contains' predicates, and not a "nearest" one. You can write a custom function to find the id of the nearest linestring for ...

View answer
Changing CRS in GeoPandas?
Accepted answer
3 votes

The reason it looks squashed like that is because GeoPandas naively plots the longitude and latitude with an equal aspect ratio. To have a more realistic result, you can convert your data to a local ...

View answer
Coordinate reference system transformation
Accepted answer
3 votes

Converting the coordinates in your geodata dataframe seems to work correctly if I use the geopandas to_crs method: In [20]: import geopandas In [21]: from shapely.geometry import Point In [26]: ...

View answer
Intersect points and polygons in shapely
Accepted answer
3 votes

I think what you want is a "spatial join", where we add information of the second dataframe (the polygons, which have the YEAR information) to the first dataframe (the points) based on their spatial ...

View answer
See the source fields of a shapefile in Geopandas?
Accepted answer
3 votes

You cannot do this in GeoPandas (apart from reading in the full file, and then you can see the column names and data types). But if you only want the schema of fields (properties) and types, you can ...

View answer
How to merge polygons that have the same values in one column in Geopandas?
3 votes

I think you want to take a look at the dissolve method, where you can group by a certain column, take the union of the geometries for each group, and do another aggregation method on the other columns....

View answer
Densify Shapely polygon using OGR Segmentize
Accepted answer
3 votes

You cannot modify the row you are iterating over directly, you either have to make a new column or to modify the original dataframe. For example: for index, row in objects.iterrows(): wkt = row['...

View answer
Geopandas Proj4 reproject to global equal area projection
Accepted answer
3 votes

The to_crs method also accepts the full proj4 string specification (or in dictionary presentation), so you can do gdf = gdf.to_crs("+proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs") ...

View answer
Ghost Multipolygons block Overlay Intersection
3 votes

The geopandas.overlay function indeed does not work on MultiPolygons. But, you can use the explode method to ensure that your dataframe does not contain any MultiPolygons. The explode method will ...

View answer