geoalchemy / geoalchemy2

Geospatial extension to SQLAlchemy
http://geoalchemy-2.readthedocs.org
MIT License
634 stars 112 forks source link

Get all 4 coordinates from POINTZM without making sql query #513

Closed chekhovana closed 4 months ago

chekhovana commented 4 months ago

Describe the problem

Hello,

I need to extract all coordinates of 4d point, preferably without sql query. I am trying to use to_shape, but it seems that 4-th coordinate is ignored during conversion.

Is it true?

Thanks in advance.

Show what you tried to do.

import os
from uuid import UUID, uuid4 as UUID4
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Mapped, mapped_column, DeclarativeBase
from geoalchemy2 import Geometry, WKBElement

class Base(DeclarativeBase):
    pass

class Location(Base):
    __tablename__ = 'location'
    id: Mapped[UUID] = mapped_column(default=UUID4, primary_key=True)
    point: Mapped[WKBElement] = mapped_column(Geometry('POINTZM'))

engine = create_engine(os.environ['DB_URL'])
Base.metadata.create_all(engine)
session = sessionmaker(engine)

from geoalchemy2.shape import to_shape

with session() as session:
    location = Location(point='POINTZM(1 2 3 4)')
    session.add(location)
    session.commit()
    point = location.point
    print('point type: ', type(point))
    print('text:', session.scalar(point.ST_AsText()))
    print('to_shape:', to_shape(point))

Describe what you expected.

actual output - to_shape returns 3 coordinates instead of 4:

point type:  <class 'geoalchemy2.elements.WKBElement'>
text: POINT ZM (1 2 3 4)
to_shape: POINT Z (1 2 3)

expected output:

point type:  <class 'geoalchemy2.elements.WKBElement'>
text: POINT ZM (1 2 3 4)
to_shape: POINT Z (1 2 3 4)

Error

No response

Additional context

No response

GeoAlchemy 2 Version in Use

0.15.1

adrien-berchet commented 4 months ago

Hi @chekhovana Unfortunately Shapely does not support M dimension (it is planned for v2.1 so after that it should work, see https://github.com/shapely/shapely/issues/1648), so it is not possible to use it to retrieve the M value. So if you don't want to use a query I'm afraid you will have to parse the WKB manually. You can find some examples on WKB parsing in https://github.com/geoalchemy/geoalchemy2/blob/master/geoalchemy2/elements.py#L163 ). Sorry I can't give more help on this :-/

chekhovana commented 4 months ago

Hi @adrien-berchet

Thank you for the information, I'll try to implement wkb parsing.