3

I am working on a tool, which transfers data, including geometries, from different legacy databases to postgis.

Currently this means I have a MS-access Database, which contains point geometries, but since access does not have a geometry type, the points consist of two columns, containing numbers, like so:

lat lon
371744,668 5700069,115

During the flow of my tool, I take these coordinates with pyodbc, and use shapely to create a point. This point does not have crs since it is newly created. Now I'm trying to set it to EPSG:25832, since the postgis DB expects geometry data to be in 25832. The code below shows what is going on. In the docs I found an example, and even when I'm directly pasting the example to my code it does not work.

What am I missing here?

def getGeomAsWKB(self, table, ids):
    tmp = self.getCoordinates(table) # tmp = list of tuples with coordinate pairs [(X,Y),(X2,Y2)...]
    geoms = []
    for coords in tmp:
        pt = Point(coords) no crs at this point, as to be expected
        srid = pyproj.CRS('EPSG:25832')
        old_srid = pyproj.CRS('EPSG:25832')
        project = pyproj.Transformer.from_crs(old_srid, srid, always_xy=True).transform
        t_pt = transform(project, pt) nothing changes
        #pt._crs = 'EPSG:25832'# i tried to set it this way, becausei did not know what else to try
        tmp_binary = wkb.dumps(t_pt)
        geoms.append(tmp_binary)
    return geoms

Just to be clear, pt and t_pt are correctly build, its just that t_pt does not contain the crs information after the transformation.

Postgis return the following error code :

Geometry SRID (0) does not match column SRID (25832)

1 Answer 1

6

Shapely geometries are unaware of their CRS. Using pyproj to transform them only changes the values of the coordinates without leaving any trace of the final CRS in the resulting geometries. Serializing a shapely geometry to wkb will never write the CRS information.

However, one option I am aware of is using GeoPandas to set the CRS and write to a file format which supports CRS information (i.e. not geojson). Some links to related methods in the geopandas docs:

It looks like shapely.wkb.dumps supports an optional srid argument which is missing from the documentation!

5
  • Okay thank you, of course serializing will never add the information, but the rest of the application works with WKB, which is derived from geometries which had a CRS, so i think it does not get lost in translation.
    – jdoe
    Commented Feb 9, 2021 at 13:16
  • @jdoe, Hmm... Perhaps this is helpful? stackoverflow.com/a/29548439/9849440 Commented Feb 9, 2021 at 14:15
  • 1
    It looks like shapely.wkb.dumps supports an optional srid argument which is missing from the documentation! Commented Feb 9, 2021 at 14:19
  • Jeremiah England, if you want to, you may post your suggestion about the missing srid argument and i will mark it as answered, since you just solved my problem. Thank you very much.
    – jdoe
    Commented Feb 10, 2021 at 7:53
  • github.com/Toblerity/Shapely/blob/… Commented Feb 11, 2021 at 7:10

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.