0

I am able to open the google KML sample by running:

ds = gdal.OpenEx( fn, 0 )

But when using a specific file that I get from my country's database, it doesn't work. I've solved the problem by converting the file into a GPX using GPSBabel but I am sure there must be a better way of doing it. By the way when I run on my .gpx:

from osgeo import gdal, ogr

# File converted from KML to GPX by GPSBabel.
fn = r"D:\Downloads\POT_DONDE_VIVE_LA_GENTE.gpx"

ds = gdal.OpenEx( fn, 0 )
cnt = ds.GetLayerCount()

for i in range( 0, cnt ):

    print( ds.GetLayer(i).GetExtent() )

I get:

(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 0.0)

That tells me that there must something wrong. The file works perfectly when using the ArcGISWebViewer.

So my questions are, how can I read KML files that output:

IOError: Could not open 'fileName'

with GDAL/OSGEO?, and if the only way is to convert them first to a different file format(how to it within python?) how can I be sure that the output of the conversion matches the original file?

2 Answers2

2

GDAL doesn't open KML files, but OGR does.

from osgeo import ogr

fn = r"D:\Downloads\POT_DONDE_VIVE_LA_GENTE.kml"
driver = ogr.GetDriverByName('KML')
dataSource = driver.Open(fn, 0) # 0 means read-only. 1 means writeable.

There are many code examples here: https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#get-shapefile-feature-count. And the file format code 'KML' can be looked up here: https://www.gdal.org/ogr_formats.html.

Paulo Raposo
  • 1,930
  • 14
  • 21
0

As user2859 said, the KML from the ArcGIS server only contains a link to the file online, and through the webpage query you can only download 100 features at a time, but using a script called AGStoShapefile I've been able to download the whole layer at a time.