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?

  • That KML doesn't contain any geometry, it contains a network link to an ArcGIS Server service. You can't open the KML with GDAL. You might be able to extract a map (picture in jpg or png format) from the WMS that the ArcGIS Server provides. – user2856 Jan 31 at 6:31
  • So there is no way of getting geo from the ArcGIS servers? – Felipe Gutierrez Jan 31 at 15:03
  • There's ~1700 features, you can grab 100 at a time using the query endpoint – user2856 Jan 31 at 20:50
  • I found a javascript script that did it for me its called AGStoShapefile. – Felipe Gutierrez Jan 31 at 22:25
  • Does anyone has a good source to converting Shapefile or GEOJson to raster? – Felipe Gutierrez Jan 31 at 22:26
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.

  • I have tried that also, and the issue is the file itself as with the code I supplied I am able to open the google sample, so that shouldn't solve the problem, but I am gonna give it a try and let you know. Thanks anyway. – Felipe Gutierrez Jan 31 at 5:35
  • 1
    Not all KMLs are vector. – user2856 Jan 31 at 7:35
  • user2856 True, they're mark-up files that might contain vector data, or raster. I've edited my answer to not imply that KML is always vector. – Paulo Raposo Feb 2 at 20:36
  • By the way you don't need ogr gdal.OpenEx() does it as well – Felipe Gutierrez Feb 2 at 22:29
  • Well not in python but if you are using the C# bindings it does make a difference – Felipe Gutierrez Feb 10 at 5:24
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.

  • I'm glad you found a solution! But for future readers' sake, this doesn't answer the original question, in which you ask how to open KML files using GDAL. – Paulo Raposo Feb 2 at 20:41
  • I've edited the question so that future readers don't get confused. – Felipe Gutierrez Feb 2 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.