1

I downloaded some Wikimapia data using the wikimapia.api. Everything is A-OK since I've opened this in Google Earth and everything is in place. However, I need to turn it into a shapefile, or get the data onto QGIS, since there are impt polygons.

Using GDAL Complete for OS X from kyngchaos, I try to run the ogr2ogr command.

$ ogr2ogr -f "ESRI Shapefile" mountains.shp api.wikimapia.org-4.kml
ERROR 4: Cannot open api.wikimapia.org-4.kml
FAILURE:
Unable to open datasource `api.wikimapia.org-4.kml' with the following drivers.

etcetc. These are named mountains which I wish to import onto QGIS.

13
  • Do you get the same result with ogrinfo? Running it with switch --debug on could give some light.
    – user30184
    Mar 3, 2015 at 5:15
  • Here is the outcome: Ryans-MacBook-Air:~ Ryan$ ogrinfo --debug [on] /Users/Ryan/Downloads/api.wikimapia.org-4.kml INFO: Open of /Users/Ryan/Downloads/api.wikimapia.org-4.kml' using driver LIBKML' successful. 1: Layer0 Ryans-MacBook-Air:~ Ryan$
    – wearashirt
    Mar 3, 2015 at 14:23
  • Ok, it found a layer. How about features: ogrinfo -al --debug on /Users/Ryan/Downloads/api.wikimapia.org-4.kml
    – user30184
    Mar 3, 2015 at 14:30
  • It shows the attribute data, AKA the mountain names. (there are quite a lot, of course). So I wonder what's bugging ogr2ogr.
    – wearashirt
    Mar 3, 2015 at 15:48
  • I would guess that you did not give the whole path to the file. At least in your question you use plain api.wikimapia.org-4.kmlinstead of /Users/Ryan/Downloads/api.wikimapia.org-4.kml
    – user30184
    Mar 3, 2015 at 15:59

2 Answers 2

4

Ogr2ogr easily handle your data but shapefile format does not. Ogrinfo reveals that your data contains one geometry which is of type Geometrycollection. Collection is made by combining one linestring and one point.

GEOMETRYCOLLECTION (
    LINESTRING (
        120.9954464 6.040449 0, 
        120.9953874 6.0402356 0, 
        120.9957951 6.0401396 0, 
        120.9958595 6.040337 0, 
        120.9958595 6.040337 0, 
        120.9954464 6.040449 0
    ), 
    POINT (
        120.9954464 6.040449 0
    ))

You have two troubles:

  1. Geometrycollections cannot be saved into shapefile.
  2. Exploding the collection to primitive elements is not enough for a rescue because linestrings and points can't be saved into a same shapefile.

For converting your KML data into shapefiles you must take two steps:

  1. Explode the collections into basic geometries
  2. Sort the geometries by the type of geometries and save lines into one shapefile and points to another.

I would do the task with ogr2ogr by using GML as an interim file format. Totally three commands are needed and you find them below. I included the non-dangerous warnings because you may not know another limitation in shapefiles: attribute names can have max. 10 characters. I apologize the cryptic layer creation options used for defining the explicit shapefile type (-lco SHPT=) but in your use case they just must be there.

ogr2ogr -f GML habib.gml habib_mosque.kmz -explodecollections

ogr2ogr -f "ESRI Shapefile" habib_point.shp habib.gml -sql "select * from habib_mosque where OGR_GEOMETRY='POINT'" -lco SHPT=POINT
Warning 6: Normalized/laundered field name: 'description' to 'descriptio'
Warning 6: Normalized/laundered field name: 'altitudeMode' to 'altitudeMo'

ogr2ogr -f "ESRI Shapefile" habib_line.shp habib.gml -sql "select * from habib_mosque where OGR_GEOMETRY='LINESTRING'" -lco SHPT=ARC
Warning 6: Normalized/laundered field name: 'description' to 'descriptio'
Warning 6: Normalized/laundered field name: 'altitudeMode' to 'altitudeMo'

Here is the end result with the outline on one layer and the reference point on another layer.

enter image description here

3
  • Amazing! Just a little more I'm almost there. I'm getting FAILURE: no data source provided. Is the sql statement supposed to refer to the gml file?
    – wearashirt
    Mar 5, 2015 at 17:52
  • Commands did work exactly as they appear and they are simply copy-pasted. Edit to suit your data. Notice that because I renamed the KML into habib_mosque.kml for avoiding the nasty space character, the layer name inside my GML is also habib_mosque. Use ogrinfo for checking layer names etc.
    – user30184
    Mar 5, 2015 at 20:48
  • The habib_mosque example was regenerated from googleearth, after saving it into Myplace in GE. The one I'm using is still directly from wikimapia API, which is named "Layer0". So I finally got things working. This was the solution, thanks so much!
    – wearashirt
    Mar 7, 2015 at 1:02
0

I have spent all day trying to find a way to reformat any of the output files from Wikimapia into a form I can load into R - and this finally has done it. Thank you!! The one key modification I had to make (other than the obvious) was "select * from Layer0"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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