Skip to main content
deleted 40 characters in body
Source Link
PolyGeo
  • 65.5k
  • 29
  • 115
  • 353

As the title of this question suggests, I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a list of coordinates coords = [0, 0, 0] # Creating a POINT M geometry point = ogr.Geometry(ogr.wkbPointM) # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library from a list of coordinates without converting them into a WKT first?

As the title of this question suggests, I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a list of coordinates coords = [0, 0, 0] # Creating a POINT M geometry point = ogr.Geometry(ogr.wkbPointM) # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library from a list of coordinates without converting them into a WKT first?

I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a list of coordinates coords = [0, 0, 0] # Creating a POINT M geometry point = ogr.Geometry(ogr.wkbPointM) # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library from a list of coordinates without converting them into a WKT first?

edited body; edited title
Source Link
Felipe D.
  • 2.9k
  • 4
  • 20
  • 39

As the title of this question suggests, I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a POINTlist Mof geometrycoordinates pointcoords = ogr.Geometry(ogr.wkbPointM)[0, 0, 0] # Creating a listPOINT ofM coordinatesgeometry coordspoint = [0, 0, 0]ogr.Geometry(ogr.wkbPointM) # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library from a list of coordinates without converting it tothem into a WKT first?

As the title of this question suggests, I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a POINT M geometry point = ogr.Geometry(ogr.wkbPointM) # Creating a list of coordinates coords = [0, 0, 0] # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library without converting it to a WKT first?

As the title of this question suggests, I am trying (and failing) to generate M-value geometries (e.g. POINT M, POINT ZM, LINESTRING M, ...) with Python's osgeo/ogr library.

Here is what I've tried to far:

import ogr # Use OGR specific exceptions ogr.UseExceptions() # Creating a list of coordinates coords = [0, 0, 0] # Creating a POINT M geometry point = ogr.Geometry(ogr.wkbPointM) # Adding the actual point data point.AddPoint(*coords) # Extracting the WKT of the newly-created geometry print(point.ExportToIsoWkt()) > POINT Z (0 0 0) 

The code above outputs a POINT Z geometry, not a POINT M geometry. The third value is being interpreted as a regular coordinate, not an M-value.

How can I avoid this?

A cumbersome workaround

I know one dirty trick that works: to use WKT instead. Here's the code:

# Creating a list of coordinates coords = [0, 0, 0] # Making a WKT string pt_wkt = f'POINT M({" ".join([str(coord) for coord in coords])})' # Converting the WKT string to a geometry pt_ogr = ogr.CreateGeometryFromWkt(pt_wkt) # Extracting the WKT back out from the geometry print(pt_ogr.ExportToIsoWkt()) > 'POINT M (0 0 0)' 

The snippet above works: it generates a POINT M geometry, as expected. However, converting a list of coordinates into a WKT can be quite cumbersome, depending on the type of geometry. Also, the creation of this ogr geometry is just going to be a small part of a larger process, so I want to avoid the conversion of a list of coordinates to a WKT string. If I can figure out the correct way to use the wkbPointM geometry (and all other M-value geometries), I might be able to speed things up significantly.

So, I am back to my main question: what is the correct way to create M-value-enabled geometries using Python's osgeo/ogr library from a list of coordinates without converting them into a WKT first?

edited title
Link
PolyGeo
  • 65.5k
  • 29
  • 115
  • 353

What is the proper way to create a Creating Python OSGEO/OGR geometry with M-values (not using WKT)?

Source Link
Felipe D.
  • 2.9k
  • 4
  • 20
  • 39
Loading