Hot answers tagged

42 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.
toms's user avatar
  • 6,928
37 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 ...
Michael Lindgren's user avatar
33 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 ...
sgillies's user avatar
  • 8,908
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 ...
gene's user avatar
  • 53.8k
24 votes
Accepted

Drivers of Fiona

you can get a list of the drivers with >>> import fiona >>> fiona.supported_drivers {'ESRI Shapefile': 'raw', 'ARCGEN': 'r', 'PCIDSK': 'r', 'SUA': 'r', 'DGN': 'raw', 'SEGY': 'r', '...
nickves's user avatar
  • 11.4k
22 votes

More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc

Additionally - geopandas now optionally includes rtree as a dependency, see the github repo So, instead of following all the (very nice) code above, you could simply do something like: import ...
claytonrsh's 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&...
user2856's user avatar
  • 59.8k
19 votes
Accepted

Intersecting two shape problem using geopandas

1) The problem is that the intersection of two polygons is not always a polygon import geopandas as gp poly1 = gp.read_file("poly_origin.shp") poly2 = gp.read_file("poly_test.shp") data = [] for ...
gene's user avatar
  • 53.8k
18 votes
Accepted

GeoPandas read_file only reading first part of KML

Document tags in KML file are separate layers for GeoPandas. When you try to get the KML content, you actually get the first layer. So you need for loop for iterating over layers. import requests ...
Kadir Şahbaz's user avatar
17 votes
Accepted

Intersecting two shapefiles from Python or command line?

The question is about Shapely and Fiona in pure Python without QGIS ("using command line and/or shapely/fiona"). A solution is from shapely import shape, mapping import fiona # schema of the new ...
gene's user avatar
  • 53.8k
15 votes

Snap points shapefile to line shapefile using shapely

The methodology is called linear referencing and a solution was given by Mike T in Coordinate of the closest point on a line with Shapely. There is also a recipe in the Python Geospatial Analysis ...
gene's user avatar
  • 53.8k
14 votes

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

I highly recommend GeoPandas for dealing with large assortments of features and performing bulk operations. It extends Pandas dataframes, and uses shapely under the hood. from geopandas import ...
songololo's user avatar
  • 1,684
14 votes
Accepted

Plot shapefile on top of raster using plot and imshow from matplotlib

I added that recipe to the rasterio documentation. Since it was such a simple shape, in this case I just unzipped the coords in the single record contained by the shapefile. That is, x, y = zip(*...
jdmcbr's user avatar
  • 882
14 votes
Accepted

GeoPandas to_file() saves GeoDataFrame without coordinate system

I have come across this behavior before. You need to explicitly pass the well known text (crs_wkt) string to the to_file() method. The string will then get passed to fiona.open(), which writes out ...
Nxaunxau's user avatar
  • 3,915
14 votes
Accepted

GeoPandas ValueError: A LinearRing must have at least 3 coordinate tuples

Figured it out after beating my head for a lot of time. Change this line gdf = gpd.read_file(layer_file) to #Read data collection = list(fiona.open(layer_file,'r')) df1 = pd.DataFrame(collection) ...
Sandeep Kumar's user avatar
12 votes

Understanding use of spatial indexes with RTree?

You've almost got it, but you've made a small error. You need to use the intersection method on the spatial index, rather than passing the index to the intersection method on the buffered point. Once ...
Snorfalorpagus's user avatar
12 votes

Rasterize a shapefile with Geopandas or fiona - python

geocube is a new tool specifically designed for rasterizing geopandas data that wraps rasterio. It simplifies the process and eliminates the need for a template raster. https://github.com/corteva/...
snowman2's user avatar
  • 6,851
12 votes
Accepted

Converting shapefile from projected coordinates using pyproj and fiona

At the first question, 'epsg:32054' code has feet units. For this reason, it is necessary to use 'preserve_units=True' as parameter in inProj = Proj(init='epsg:32054') line. Now, next code works well: ...
xunilk's user avatar
  • 28.4k
11 votes
Accepted

How to add support for FileGDB (Esri file gdb API) driver in fiona?

The Gohlke GDAL/OGR wheel includes the FileGDB driver compiled as a plugin. To get the FileGDB driver working: Copy the Esri bin64\FileGDB.dll to [python install/virtualenv dir]\Lib\site-packages\...
user2856's user avatar
  • 59.8k
11 votes
Accepted

Computing convex hull of points using shapely

To transform the shapefile geometries to shapely geometries (you can't build a MultiPoligon with a single point as in your solution) from shapely.geometry import Point, shape import fiona for feat ...
gene's user avatar
  • 53.8k
10 votes
Accepted

Adding column to shapefile with Fiona?

It seems to me that you have some problems with Fiona. Remember that all the elements are dictionaries 1) you define a new field schema['properties']['pi'] = 'float' 2) this field must be in in the ...
gene's user avatar
  • 53.8k
9 votes
Accepted

Geopandas Polygon to matplotlib patches Polygon conversion

There is a Python module for that: Descartes (look at Plot shapefile with matplotlib for example) from geopandas import GeoDataFrame test = GeoDataFrame.from_file('poly1.shp') test.set_index('id', ...
gene's user avatar
  • 53.8k
9 votes
Accepted

Open GPKG embedded layers in python

Use the layer parameter: gdf = gpd.read_file(r'C:\folder\geopackage.gpkg', layer='somelayername') read_file use fiona.open, see: fiona.open docs
BERA's user avatar
  • 63.6k
9 votes
Accepted

Listing every layer in Geopackage using Fiona

It's simple to do this using Fiona. I prefer using GeoPandas to read the Geopackage, and then Fiona to list the layers: import geopandas as gpd import fiona gpkg = 'path/to.gpkg' layers = fiona....
Encomium's user avatar
  • 2,972
8 votes
Accepted

How can I extract contours from a raster with Python?

I'd recommend using gdal_contour . The results are likely to be much better than any attempt to re-implement it :-) Having said that, there should be a way to do this in rasterio. I've not tried ...
Steven Kay's user avatar
  • 20.2k
8 votes
Accepted

How to get all records if WFS output limited to 1000?

Check the service capabilities https://geoservices.noord-holland.nl/ags/services/oi_dataservice_alg/MapServer/WFSServer?request=GetCapabilities&service=WFS&version=2.0.0 <ows:Constraint ...
user30184's user avatar
  • 60.1k
7 votes
Accepted

Select by attribute using fiona

Much more easily: with fiona.open("a_shapefile.shp") as input: meta = input.meta with fiona.open('a_shapefile2.shp', 'w',**meta) as output: for feature in input: if ...
gene's user avatar
  • 53.8k
7 votes

How to find the intersection areas of overlapping buffer zones in single shapefile?

The problem here is that you need to iterate over two polygons of the layer of polygons as you do when you iterate through two layers (look at intersecting two shapefiles from Python or command line) ...
gene's user avatar
  • 53.8k
7 votes
Accepted

Find common borders of features and get their attributes using fiona and shapely

As Fiona works with dictionaries, it is easy. With the example of Finding the common borders between polygons in the same shapefile import fiona from shapely.geometry import shape, mapping # load ...
gene's user avatar
  • 53.8k
7 votes
Accepted

Only read specific rows of a shapefile with GeoPandas / Fiona

If you don't want to read the whole file, another solution is to use a generator as in Only read specific attribute columns of a shapefile with Geopandas / Fiona As a reminder (from What does the “...
gene's user avatar
  • 53.8k

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