1

I have this code:

driver = ogr.GetDriverByName('KML')
data = driver.Open(kmlFile)
layer = data.GetLayer()

geom = nextFeature.GetGeometryRef()

print(geom.ExportToJson())

I am trying to export any kind of output like GeoJSON or WKT but;

{ "type": "MultiPolygon", "coordinates": [ [ [ [ 33.228451, 39.725639, 0.0 ], [ 33.18737, 39.653671, 0.0 ], 
[ 33.234299, 39.644131, 0.0 ], [ 33.280369, 39.671131, 0.0 ]

the output is something like this and looks like the geometry that comes from KML file is in 3D projection or something, or at least the program thinks it is.

Here is the spatial reference if you need to know:

{
  "$schema": "https://proj.org/schemas/v0.2/projjson.schema.json",
  "type": "GeographicCRS",
  "name": "WGS 84",
  "datum": {
    "type": "GeodeticReferenceFrame",
    "name": "World Geodetic System 1984",
    "ellipsoid": {
      "name": "WGS 84",
      "semi_major_axis": 6378137,
      "inverse_flattening": 298.257223563
    }
  },
  "coordinate_system": {
    "subtype": "ellipsoidal",
    "axis": [
      {
        "name": "Latitude",
        "abbreviation": "lat",
        "direction": "north",
        "unit": "degree"
      },
      {
        "name": "Longitude",
        "abbreviation": "lon",
        "direction": "east",
        "unit": "degree"
      }
    ]
  },
  "id": {
    "authority": "EPSG",
    "code": 4326
  }
}
3
  • 1
    That's not weird at all. KML coordinates can have X, Y, and Z. For example in developers.google.com/kml/documentation/kml_tut A Point that specifies the position of the Placemark on the Earth's surface (longitude, latitude, and optional altitude). That's 2.5D, not really 3D. If you want you can tell GDAL to save data as 2D and drop the Z used for height.
    – user30184
    Jan 5, 2021 at 14:14
  • Its actually on 2D plane but I guess the KML format forces it to be that way, I only want the XY coordinates, how can I save it as 2D? Jan 5, 2021 at 14:16
  • If you used ogr2ogr gdal.org/programs/ogr2ogr.html you would do it with -dim XY or alternatively with another expression but identical meaning -dim 2. The Python way is certainly documented somewhere.
    – user30184
    Jan 5, 2021 at 14:19

1 Answer 1

1

I found that you can use .FlattenTo2D() method on geometry objects and that's it.

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