Hot answers tagged

36 votes
Accepted

What does OGR stand for?

OpenGIS Simple Features Reference Implementation. You can read a short statement about the history of the abbreviation among some other GDAL related facts in the GDAL FAQ. The relevant paragraph ...
user avatar
  • 4,398
32 votes
Accepted

Importing shapefile to PostGIS using Python and ogr

In pure Python, without using the subprocess module (os.system is deprecated) to call ogr2ogr or shp2pgsql, for example): 1) with ogr Append a shapefile to a postgis table using the GDAL/OGR Python ...
user avatar
  • 50.7k
31 votes
Accepted

Check if a point falls within a multipolygon with Python

Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one feature = list of polygons, look at Converting huge multipolygon to polygons) The ...
user avatar
  • 50.7k
28 votes
Accepted

Join CSV file to shapefile using gdal/ogr

The ogr2ogr utility supports a limited sql syntax. You can join your CSV to the shapefile using something like the following: ogr2ogr -sql "select inshape.*, joincsv.* from inshape left join '...
user avatar
  • 51.7k
27 votes

Installing GDAL and OGR for Python?

Only adding this because I tried using the kyng chaos tools, but on my Mac OS X machine I was able to very, very easily install this with Anaconda conda install gdal Posting in case anyone finds ...
25 votes

Opening ESRI Personal GeoDatabase (*.mdb) in QGIS

If you have installed a 64-bit version of QGIS on Windows and you find that Personal GeoDatabases (*.mdb) no longer work for you, then this solution might apply; I'm on QGIS 2.8.1 rather than 2.6, but ...
user avatar
  • 11.8k
23 votes
Accepted

Uploading Geopackage contents to PostgreSQL

This tutorial shows how to import geopackges into postgres with ogr2ogr like: ogr2ogr -f PostgreSQL "PG:dbname=gadm" DNK_adm.gpkg Note importing into PostgreSQL like this will convert the case of ...
user avatar
  • 982
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
user avatar
  • 823
19 votes
Accepted

Determining if shapefile and raster overlap in Python using OGR/GDAL?

The following script determines the bounding box of a raster and creates based on the bounding box a geometry. import ogr, gdal raster = gdal.Open('sample.tif') vector = ogr.Open('sample.shp') # ...
user avatar
  • 7,624
18 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 ...
user avatar
17 votes

Converting CSV file of WKT data to shapefile using ogr2ogr?

I had to solve the same problem today, so here is my answer, which gives a complete solution. I have a lineWKT.csv file stored in F:\Data\ folder, with the data like this: id,gm 0,"LINESTRING (30 10 ...
user avatar
13 votes

Join CSV file to shapefile using gdal/ogr

The accepted answer is really useful, but I found that it was slow with a large-ish database. I believe it also limits your options when joining the data. My method now is to pull everything into ...
user avatar
13 votes

Opening ESRI Personal GeoDatabase (*.mdb) in QGIS

Provided you have successfully installed Microsoft Access Database Engine 64 bit, you can: Method 1 (works with QGIS 3) In QGIS Settings panel -> Options | System | Environment add the following ...
user avatar
13 votes
Accepted

select by location using ogr2ogr sqlite

At the following link all SQL functions that test spatial relationships are described: http://www.gaia-gis.it/spatialite-2.4.0/spatialite-sql-2.4.html#p12. The following spatial relationships can be ...
user avatar
  • 886
12 votes

No ogr module despite having gdal installed?

Use these commands: sudo apt-get install python-gdal sudo apt-get install gdal-bin
user avatar
  • 131
12 votes
Accepted

PostGIS: parse geometry wkb with OGR

Internally, PostGIS stores geometries in a binary specification, but it is queried and viewed outside as a hex-encoded string. There are two popular variations of well-known binary (WKB): EWKB (via ...
user avatar
  • 39.7k
12 votes
Accepted

MultiLineString to separate individual lines using Python with GDAL/OGR, Fiona, Shapely

A MultiLineString is a list of lines: from shapely.geometry import MultiLineString, mapping, shape coords = [((0, 0), (1, 1)), ((-1, 0), (1, 0))] lines = MultiLineString(coords) print lines ...
user avatar
  • 50.7k
12 votes
Accepted

Remove 3D geometry in shapefile using OGR

Write your data into a new shapefile with ogr2ogr and force geometry type into 2D with the -dim switch. ogr2ogr -f "ESRI Shapefile" output_2d.shp input_zm.shp -dim 2 The -dim setting is not limited ...
user avatar
  • 51.5k
10 votes

Convert a spatial polygon object to data frame using R

Problems: 1: the outcome of UnionSpatialPolygons is a spatial polygon 2: converting the result back into a spatial polygon data frame is a real pain -a. you need a very exact data frame to attach ...
user avatar
  • 303
10 votes
Accepted

Equivalent function to shapely's envelope in ogr?

The Ogr function GetEnvelope() returns "a tuple (minX, maxX, minY, maxY)" (from here), but what you want (from what I can understand) is a Polygon describing the envelope/bbox? This is actually ...
user avatar
  • 4,241
10 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\...
user avatar
  • 51.7k
10 votes
Accepted

Why Editing a GeoPackage table with ogr is very slow?

Make a minor but important change and make all edits to happen within one transaction instead of doing one transaction for each row. Transactions are rather expensive for SQLite and therefore also for ...
user avatar
  • 51.5k
10 votes
Accepted

Where is official documentation of gdal/ogr?

You can find official GDAL documentation at https://gdal.org/ All classes documentations are here: C/C++ API - https://gdal.org/hierarchy.html Python API - https://gdal.org/python/ Also you can ...
user avatar
9 votes
Accepted

Ogr2ogr - shape to postgis - is there a way to use and preserve an existing primary key?

You can add the -lco FID=[your_id_column] switch to your OGR2OGR script. Eg. ogr2ogr -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.shp -lco FID=id ...
user avatar
9 votes

PostGIS: parse geometry wkb with OGR

Within the database, geometries are stored on disk in a format only used by the PostGIS program. In order for external programs to insert and retrieve useful geometries, they need to be converted into ...
user avatar
  • 50.7k
9 votes
Accepted

Loading virtual vector layer in QGIS

You need to either add relativeToVRT="1" to the SrcDataSource element as it defaults to an absolute path or use an absolute path to the vector files. From the VRT driver page: The element may ...
user avatar
  • 51.7k
9 votes

How to make a local copy of a large WFS dataset?

The easiest way to do this, is to use GDAL's ogr2ogr utility. Firstly, you need to create an XML file with the URL to the WFS endpoint. <OGRWFSDataSource> <URL>http://demo.opengeo.org/...
user avatar
9 votes
Accepted

ogr2ogr sql query from text file

It is understandable to read only the manual page of ogr2ogr http://www.gdal.org/ogr2ogr.html and miss the page about options which are general to all OGR utilities https://gdal.org/programs/...
user avatar
  • 51.5k
9 votes

Check if a point falls within a multipolygon with Python

The problem in your first example is in this loop: ... shpfilePoints = [] for shape in polygon: shpfilePoints = shape.points ... It only appends the last feature points. I tried out my approach ...
user avatar
  • 25.8k

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