Skip to content

register_shapely ignores SRID causing SRID missmatch errors #1028

@Zaczero

Description

@Zaczero

register_shapely ignores SRID when dumping geometries.

I was able to resolve the issue by updating the dumper logic as follows:

class BaseGeometryBinaryDumper(Dumper):
    format = Format.BINARY

    def dump(self, obj: BaseGeometry) -> Buffer:
        return to_wkb(obj, include_srid=True)


class BaseGeometryDumper(Dumper):
    def dump(self, obj: BaseGeometry) -> Buffer:
        return to_wkb(obj, True, include_srid=True).encode()

For geometries without SRID, the output remains unchanged. I have switched to using to_wkb (which is a wrapper around dumps) for better performance. The Buffer | None type hint was also incorrect because this overridden method never returns None.

While working on this, I made some small optimizations to the loader:

class GeometryBinaryLoader(Loader):
    format = Format.BINARY

    def load(self, data: Buffer) -> BaseGeometry:
        return from_wkb(bytes(data))


class GeometryLoader(Loader):
    def load(self, data: Buffer) -> BaseGeometry:
        # it's a hex string in binary
        return from_wkb(bytes(data))

I replaced loads usage with from_wkb (which is a wrapper around loads). In the binary case, it avoids an isinstance call + conditional check, as bytes(bytes) is very efficient and avoids copying in the CPython implementation. In the text case, it avoids bytes copy + conditional check, and simply reuses the binary implementation: from my testing, from_wkb seems to be very flexible, accepting bytes, hex strings, and hex bytes:

Image

Note

If you're planning to use any part or all of the suggested code, please add me to the commit's Co-authored-by list. I enjoy collecting contributions to open source projects 🙂.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions