Географические информационные системы и дистанционное зондирование

Extract raster values at vector points location with Python/GDAL

Обсудить в форуме Комментариев — 58Редактировать в вики

Эта страница опубликована в основном списке статей сайта
по адресу

Script to extract raster values at points.

Extracting data from a single raster (or several rasters) using a set of vector point objects is a fairly common task. This is necessary when we want to construct the spectral profiles, quantify the accuracy of raster data, inspect changes or prepare training datasets for classification, etc.


Here is already a similar tool, on our website, but it was developed for ArcView and has several limitations.


[править] Preparation

To use this tool you need our python script, Python 2.5 or higher and GDAL library with Python bindings.

The simplest way to get all necessary components under OS Windows is to use OSGeo4W installer:

  1. Download installer. If the previous link is not accessible, there is an alternative link on a mirror website
  2. Run the installer
  3. Select «Advanced install» and press «Next»
  4. Select gdal and gdal-python packages (under Libs category), then press «Next»
  5. Wait while requested packages and their dependencies are installed

If you run Linux, then install all necessary packages using your package manager. In Ubuntu/Debian you need to install python-gdal package.

Extract the script from archieve and copy into any directory in your PATH (for example, into C:\Tools\GIS or ~/bin). To run the script under Windows, you need to open OSGeo4W shell and run the script from the shell.

[править] Usage

There is no graphical interface for the script and it runs in console mode at the moment. It takes several arguments as input and output [-c] [-r] point_shapefile [raster_file(s)] [-d directory_with_rasters]
  • point_shapefile — vector point in ESRI shapefile format. Raster values will be extracted at these locations
  • raster_file(s) — raster(s) from which values will be extracted. If rasters and script located in different directories, full path should be specified. Use space to separate multiple rasters
  • -c — write data into external file. A CSV file (with same name as shapefile) will be created within the input shapefile folder, in this file all attributes and extracted data will be written. In this mode input shapefile remains unchanged
  • -r — use this switch to reproject points into raster CRS, when rasters and vector are in different CRS
  • -d — directory processing mode. After this switch a path to directory with rasters should be specified. Data will be extarcted from all rasters in this directory

The script works with single- and multiband raster in all GDAL-compatible formats. For each single-band raster in point shapefile, a new field will be created. The name of this field is is identical to raster name. For multiband rasters the field name in DBF file will consists of raster name and band number.

Important! You must remember that the field name in the DBF format is limited to 10 characters, hence long names will be truncated.

Here is result of the data extraction from 6-bands Landsat fragment (raster data type is Byte, raster filename — clearcuts_174016.tiff), opened in QGIS. For the initial two fields (id, taxon) new fields clearcut_1 - clearcut_6 (see, that filed names are truncated due to the DBF format limitations) with corresponding values were added.\


[править] Examples

Extract values from raster after.tiff using points from shapefile poi.shp (script and files are in the same directory) poi.shp after.tiff

Extract values from raster after.tiff using points from shapefile poi2.shp and write result into external file -с poi2.shp after.tiff

Extract values from raster after.tiff using points from shapefile poi2.shp, raster and vector are in different CRS (so we use -r switch to enable reprojection) -r poi2.shp after.tiff

Extract values from rasters before.tiff and after.tiff using points from shapefile points.shp points.shp before.tiff after.tiff

Extact values from all rasters in directory using points from shapefile points.shp points.shp -d D:\data\rasters_veg

[править] Links

Обсудить в форуме Комментариев — 58Редактировать в вики

Последнее обновление: 2016-06-22 20:02

Дата создания: 13.07.2011
Автор(ы): Александр Бруй, Максим Дубинин


Если Вы обнаружили на сайте ошибку, выберите фрагмент текста и нажмите Ctrl+Enter