As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

66 votes

How to get raster corner coordinates using Python GDAL bindings?

This can be done in far fewer lines of code src = gdal.Open(path goes here) ulx, xres, xskew, uly, yskew, yres = src.GetGeoTransform() lrx = ulx + (src.RasterXSize * xres) lry = uly + (src....
James's user avatar
  • 2,218
49 votes

How to call gdal_translate from Python code?

Since GDAL 2.1 (more info here), GDAL and OGR utilities can be used as library functions. For instance: from osgeo import gdal ds = gdal.Open('input.tif') ds = gdal.Translate('output.tif', ds, ...
Antonio Falciano's user avatar
49 votes
Accepted

Creating transparent hillshade?

Nutshell Each set of 3 images below should be read such as "grey (band) + opacity (band) = transparent result". You can test these processes within minutes via the associated github hosted ...
Hugolpz's user avatar
  • 2,633
46 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 ...
gene's user avatar
  • 53.8k
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 ...
Marciano Saraiva's user avatar
36 votes

Merging all tiles from one directory using GDAL

You could create a virtual mosaic from all Tiff files: gdalbuildvrt mosaic.vrt c:\data\....\*.tif and convert it afterwards: gdal_translate -of GTiff -co "COMPRESS=JPEG" -co "PHOTOMETRIC=YCBCR" -co ...
christoph's user avatar
  • 4,721
33 votes
Accepted

Get field names of shapefiles using GDAL

1) individual shapefile: as in the comment, a shapefile has only one layer. If you want only the names of the fields from osgeo import ogr source = ogr.Open("a_shapefile.shp") layer = source.GetLayer(...
gene's user avatar
  • 53.8k
32 votes

Scale 16 bit to 8 bit within range using GDAL

If you don' want the values above 255 to be cut, you need to scale them down. For that purpose gdal_translate provides the option -scale: From the Manual: -scale [src_min src_max [dst_min dst_max]]...
pLumo's user avatar
  • 6,329
32 votes
Accepted

Using Rasterio or GDAL to stack multiple bands without using subprocess commands

Using rasterio you could do import rasterio file_list = ['file1.tif', 'file2.tif', 'file3.tif'] # Read metadata of first file with rasterio.open(file_list[0]) as src0: meta = src0.meta # ...
Loïc Dutrieux's user avatar
28 votes
Accepted

GDAL scripts not found in QGIS 3 on OSX

This is a known issue as documented on KyngChaos https://www.kyngchaos.com/blog/2018/20180322_qgis_3.0.0-3 A workaround is available as follows Go to Settings ... Options... System ... Environment ...
spatialthoughts's user avatar
25 votes
Accepted

Reading, modifying and writing a geotiff with GDAL in python

Your script is missing the ds.FlushCache method, that saves to disk what you have in memory at the end of the modifications. See below a corrected version of your example. Notice that I also added two ...
Andrea Massetti's user avatar
25 votes

Installing GDAL2.1

You can download GDAL 2.1 for Windows from GIS Internals. There is an installer and a portable version that doesn't require installation. GDAL 2.1 is available for Ubuntu 16.04 from the UbuntuGIS-...
user2856's user avatar
  • 60k
25 votes
Accepted

How to efficiently access files with GDAL from an S3 bucket using VSIS3?

I've found when something isn't particularly well documented in GDAL, that looking through their tests can be useful. The /vsis3 test module has some simple examples, though it doesn't have any ...
user2856's user avatar
  • 60k
25 votes
Accepted

Extracting EPSG from a raster using gdal bindings in Python

I found the following workaround. I am unsure if it is the most efficient, but it does work for me. from osgeo import gdal, osr path = r"C:\temp\test2.tif" d = gdal.Open(path) proj = osr....
Lennert De Feyter's user avatar
24 votes

Python equivalent of gdalbuildvrt

The answer of @rcoup only worked for me, if modify it as follows: from osgeo import gdal vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True) my_vrt = gdal.BuildVRT('my.vrt', ['...
JensL's user avatar
  • 341
24 votes

Extract raster values within shapefile with pygeoprocessing or gdal

You can use rasterio to extract the raster values within a polygon as in GIS SE: GDAL python cut geotiff image with geojson file I use here a one band raster file and GeoPandas for the shapefile ( ...
gene's user avatar
  • 53.8k
23 votes
Accepted

Removing small holes from the polygon

You can use the following example script. The script removes all holes from a polygon whose area is greater than eps. from shapely import wkt from shapely.geometry import Polygon # sample polygon ...
Kadir Şahbaz's user avatar
22 votes

Splitting raster into smaller chunks using GDAL?

There's a bundled python script specifically for retiling rasters, gdal_retile: gdal_retile.py [-v] [-co NAME=VALUE]* [-of out_format] [-ps pixelWidth pixelHeight] [-overlap ...
mikewatt's user avatar
  • 4,718
22 votes

gdal ImportError in python on Windows

Condensed procedure outlined in http://cartometric.com/blog/2011/10/17/install-gdal-on-windows/ for Windows 7, 32 Bits, to install GDAL PYTHON: 1) Install Python. I installed Python 2.7.9 from https:...
xunilk's user avatar
  • 28.6k
22 votes
Accepted

Installing GDAL on MacOSX?

So far brew install gdal --HEAD resulted in $ ogr2ogr --version GDAL 2.1.0dev, released 2015/99/99
tato's user avatar
  • 823
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) &...
Mattijn's user avatar
  • 956
20 votes

Generating random coordinates in multipolygon in Python?

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons The polygon in code below must be Polygon object. import random from shapely.geometry import Point def generate_random(...
dmh126's user avatar
  • 6,542
20 votes

Retrieve pixel value with geographic coordinate as input with gdal

You can retrieve raster pixel values with any of the following scripts. List of X,Y coordinates (as tuples) are in the python list named 'points_list'. With Python GDAL: from osgeo import gdal ...
xunilk's user avatar
  • 28.6k
20 votes
Accepted

ogr2ogr merge multiple shapefiles: What is the purpose of -nln tag?

For GDAL there are datastores which contain layers. Some datastores, like the database ones or GML, can hold several layers but some others like shapefiles can only contain one layer. You can test ...
user30184's user avatar
  • 60.3k
20 votes
Accepted

Finding pixel location in raster using coordinates?

A rasterio way of doing this is pretty simple. Note this requires your raster be in the same projection as your coordinates. You can of course project your coordinates on the fly, but that's another ...
user2856's user avatar
  • 60k
19 votes
Accepted

Convert huge XYZ CSV to GeoTIFF

You can do this using GDAL, it directly supports XYZ format. It doesn't matter if your coordinates are UTM, gdal_translate will output in the same coordinate system. So to convert to GeoTIFF is as ...
user2856's user avatar
  • 60k
19 votes
Accepted

Calculate total area of polygons in shapefile using GDAL?

There's a special field in OGR SQL called OGR_GEOM_AREA which returns the area of the feature's geometry: ogrinfo -sql "SELECT SUM(OGR_GEOM_AREA) AS TOTAL_AREA FROM myshapefile" myshapefile.shp ...
Antonio Falciano's user avatar
18 votes

Accessing feature classes in file geodatabases using Python and GDAL

Much simpler and intuitive if you use fiona and geopandas import fiona import geopandas as gpd # Get all the layers from the .gdb file layers = fiona.listlayers(gdb_file) for layer in layers: ...
Solomon Vimal's user avatar
18 votes
Accepted

How does QGIS open so large raster datasets (about 40GB)?

If QGIS is runnig in a 1000x1000 pixel sized window on your screen there is no need to read all 32000x32000 pixels for showing the map. GDAL tries to read data from the source image so that no data at ...
user30184's user avatar
  • 60.3k

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