Wednesday, 5 October 2016

gdal - Converting Raster Data to Polygon data


I am trying to convert a raster data to polygon data. Previously I was using the shapefile.py to create point data on individual lat/lon on the raster. But that will create a Huge data and querying it will be a Big issue.


So, it would be better if I am able to convert the raster data into a polygon data. I was able to find SAGA, but it seems that it can only convert the .grd files. Is there a way to convert GeoTIFF format or raster datasets in general to Polygon?


UPDATE: Using gdal_polygonize.py


I am executing the following line of code,


gdal_polygonize.py ~/development/Biodiversity/biodiversity/GeoTIFF/bio11.tif -f "ESRI Shapefile" bio11.shp

But I am getting the following error. My gdal version is 1.9.0.


Traceback (most recent call last):

File "/usr/bin/gdal_polygonize.py", line 34, in
import gdal, ogr, osr
File "/usr/lib/python2.7/dist-packages/gdal.py", line 2, in
from osgeo.gdal import deprecation_warn
File "/usr/lib/python2.7/dist-packages/osgeo/__init__.py", line 21, in
_gdal = swig_import_helper()
File "/usr/lib/python2.7/dist-packages/osgeo/__init__.py", line 17, in swig_import_helper
_mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: libjvm.so: cannot open shared object file: No such file or directory

Answer




Try gdal_polygonize.py. E.g., to create a Shapefile mylayer.shp from input myraster.tif:


gdal_polygonize.py myraster.tif -f "ESRI Shapefile" mylayer.shp

or to output directly to a PostGIS database (see the PostgreSQL / PostGIS driver details for GDAL):


gdal_polygonize.py myraster.tif -f PostgreSQL PG:"dbname='postgis' user='postgres'" mylayer

(ignore the "ERROR 1: No field definitions found for 'mylayer', is it a table?" message)


There are Python bindings underneath to GDALPolygonize, if you need this tool customized.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...