64
votes
Reading shapefile in Python
I find GeoPandas as the best performer here.
import geopandas as gpd
shapefile = gpd.read_file("shapefile.shp")
print(shapefile)
54
votes
Accepted
How to polygonize raster to shapely polygons
Use rasterio of Sean Gillies. It can be easily combined with Fiona (read and write shapefiles) and shapely of the same author.
In the script rasterio_polygonize.py
the beginning is
import rasterio
...
47
votes
Accepted
how to take cell size from raster using python or GDAL or RASTERIO
With PyQGIS
ras = QgsRasterLayer("raster.tif")
pixelSizeX= ras.rasterUnitsPerPixelX()
pixelSizeY = ras.rasterUnitsPerPixelY()
print pixelSizeX
2.11668210081
print pixelSizeY
2.11685012701
With GDAL
...
36
votes
Accepted
Import error: no module named _gdal_array
Make sure you have numpy installed before you attempt to install the GDAL Python bindings; without numpy, it appears the _gdal_array native code will not be installed.
If you ended up in this ...
21
votes
Raster reclassify using python, gdal and numpy
Instead of doing the reclassification as a double for loop described by dmh126, do it using np.where:
# reclassification
lista[np.where( lista < 200 )] = 1
lista[np.where((200 < lista) &...
13
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/...
11
votes
Raster reclassify using python, gdal and numpy
Here's a basic example using rasterio and numpy:
import rasterio as rio
import numpy as np
with rio.open('~/rasterio/tests/data/rgb1.tif') as src:
# Read the raster into a (rows, cols, depth) ...
11
votes
Accepted
Rasterizing shapefiles with GDAL and Python?
I tried out your code, slightly modified by me for adapting it to mi data (adding your 'hedgerow' field to my shapefile), and it ran well with only add to your code the line: target_ds = None.
from ...
11
votes
Accepted
Copy attributes from existing shapefile to new shapefile (Python 2.7.13, GDAL)
1) With ogr, see Fast tip: Filtering features using OGR Python for example
from osgeo import ogr
from os import remove
in_file= "shapefileA.shp"
out_file = "shapefileB.shp"
in_ds = ogr.Open( in_file )...
10
votes
Making ArcPy interactive?
Python window is not an equivalent of the Python shell; hence, you won't be able to use the raw_input there. To implement the interactivity with the user, you may choose any of these alternatives:
...
9
votes
Raster reclassify using python, gdal and numpy
Just to complete the answer from @Mattijn, I think that will lead into a problem if the input classes overlap with the output classes. I don't want my new value to be changed by the next rule.
I don'...
9
votes
Converting between coordinate systems for LAS file using LASPY
You should check what the scale and offset are for your file. This can be done as follows:
van_taken.header.scale
van_taken.header.offset
This almost looks like an overflow error to me. The lower ...
9
votes
Accepted
Error using pip install for arcpy: "Could not find a version that satisfies the requirement arcpy...."
arcpy ships with Esri's ArcGIS software package. It is neither free nor open source and, even if you were able to obtain it without an ArcGIS license, you would not be able to use it as it has a built-...
8
votes
Accepted
Raster reclassify using python, gdal and numpy
Here you have a simple python script for reclassification, I wrote it and it works for me:
from osgeo import gdal
driver = gdal.GetDriverByName('GTiff')
file = gdal.Open('/home/user/workspace/raster....
8
votes
How to polygonize raster to shapely polygons
Here is my implementation.
from osgeo import ogr, gdal, osr
from osgeo.gdalnumeric import *
from osgeo.gdalconst import *
import fiona
from shapely.geometry import shape
import rasterio.features
#...
8
votes
Accepted
" ".join(list) not inserting spaces when calculating field
If you want to use the Calculate Field tool (instead of an Update Cursor), what you're assigning to val needs to be an unevaluated python expression. That is, it needs to be identical to the string ...
8
votes
Accepted
How to iterate through all fields, and all records in each field with GDAL/OGR
The reason for this behavior is that ogr reads the features of layer sequentially and updates the "read position" each time. After reading through layer once, the "read position" is at the end of the ...
8
votes
SQL queries in PostGIS database layer using Python and QGIS
Adding PostGIS table to a QGIS map
In a QGIS plugin I would use QgsDataSourceURI from the pyqgis API. Here is some snippet form a project I worked on.
Database connection is wrapped in a function. ...
8
votes
Accepted
Saving python Rtree spatial index to file?
Is the built-in disk serialization an acceptable solution?
Make an index and flush it to disk:
from rtree.index import Index
idx = Index('filename')
idx.insert(0, (1, 2, 3, 4))
idx.get_bounds()
# [1....
8
votes
Accepted
Using tempfiles for intermediate dataset in ArcPy?
You cant have a file extension for rasters in the in_memory workspace so change slope = "in_memory/slope.tif" to slope = "in_memory/slope"
See:
001167: No extension is allowed for in-memory raster ...
8
votes
Accepted
Creating QGIS copy-paste features between two layers using PyQGIS?
To copy all features; create a list, get features from source layer, create data provider on destination layer, and finally add features to destination layer:
sourceLYR = QgsMapLayerRegistry.instance(...
8
votes
Accepted
python GDAL 'GetDriverByName', argument 1 of type 'char const *'
Because you imported unicode_literals, you're passing a unicode literal to GDAL which is expecting a string literal.
So, explicitly cast the 'GTiff' arg to a str.
E.g.
>>> from ...
7
votes
Import shapefile is not working in pyshp?
Try pip install pyshp in your command prompt before trying to run import shapefile.
7
votes
Accepted
Reclassify a raster value to -9999 and set it to the nodata value using python and or gdal
If you are looking for an easy way to change specific pixels to NoData (Nan) or -9999 please take a look at this alternative script I wrote:
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 12 21:33:...
7
votes
Using ArcPy on MacBook?
ArcPy is a site package bound to a release of Python associated with ArcGIS Desktop (Windows -- 32-bit or 64-bit, with Background Geoprocessing installed) or ArcGIS Server (Windows and Linux -- 64-bit)...
7
votes
RTree spatial index does not result in faster intersection computation
My answer is essentially based on another answer by @gene here:
More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc
He proposed the same solution using two differents methods, ...
7
votes
Accepted
Catching arcgisscripting.ExecuteError and error details?
I suspect what you actually want to catch is an arcpy.ExecuteError (or have you imported arcgisscripting somewhere in your code?)
except arcpy.ExecuteError:
print arcpy.GetMessages()
# Handle ...
7
votes
Accepted
Why does osr.SpatialReference return different values in python2 and 3?
The projections differ in the datum shift values for the conversion from Amersfoort datum to WGS84.
The first one is tfm code 4833, and the second is tfm code 15934.
GDAL 2.1.0 uses tfm code 4833, ...
6
votes
Reading/writing utf-8 encoded data using Fiona?
Fiona read ok utf-8 files without add nothing.
Try encoding='utf-8' only when output with fiona.
So change...
with fiona.open(out_file, 'w', **meta) as output:
To
with fiona.open(out_file, 'w', ...
6
votes
How to run in background an .exe program withArcGIS?
You can try to use a python script and use one of the two following modules. But think of it carefully as running a program out of a script can do many unpleasent things.
import subprocess
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python-2.7 × 696arcpy × 238
pyqgis × 142
python × 115
qgis × 97
gdal × 64
arcgis-desktop × 60
arcgis-10.1 × 55
arcgis-10.2 × 44
qgis-plugins × 44
arcmap × 36
qgis-2 × 28
shapely × 25
cursor × 22
installation × 21
raster × 20
ogr × 20
numpy × 19
shapefile × 17
grass-gis × 15
coordinate-system × 14
python-addin × 14
arcgis-10.3 × 12
qgis-processing × 11
fiona × 11