Comments (4)
Hi @rmmariano
I just took a quick look but as far I can see you mixed core and ORM queries. If you want to use core queries, you can look at https://geoalchemy-2.readthedocs.io/en/latest/gallery/test_length_at_insert.html#sphx-glr-gallery-test-length-at-insert-py, while if you want to use ORM queries you can look at https://geoalchemy-2.readthedocs.io/en/latest/gallery/test_type_decorator.html#sphx-glr-gallery-test-type-decorator-py.
Basically, with core queries you should use func.ST_*
and bindparam
in the insert queries while with ORM queries you should create a specific type that will insert the proper func.ST_*
automatically at insert.
from geoalchemy2.
Hi @adrien-berchet
Thank you for you awnswer.
I've read the example, but it's not clear to me how I could update my example to use ORM.
I'm newbie on geoalchemy.
Could you please send my an example based on the one that I made? Using ORM, when you have free time.
Thanks a lot.
from geoalchemy2.
Hi @rmmariano
Here is an example of what I spoke about. As I said, you have to create a specific type for your bbox
column that automatically use ST_Envelope
at insert. I also added an example with core inserts in the end. I hope it will solve your issue.
from geoalchemy2 import Geometry, WKBElement, WKTElement
from sqlalchemy import Integer, create_engine, bindparam, select
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, sessionmaker
from sqlalchemy.sql import func
from sqlalchemy.types import TypeDecorator
db_connection_url = "postgresql://gis:gis@localhost/gis"
engine = create_engine(url=db_connection_url, echo=True)
class BBoxGeometry(TypeDecorator):
"""This class is used to insert a ST_Force3D() in each insert."""
impl = Geometry
cache_ok = True
def bind_expression(self, bindvalue):
return func.ST_Envelope(
self.impl.bind_expression(bindvalue),
type=self,
)
class Base(DeclarativeBase):
...
class Geofence(Base):
__tablename__ = "geofence"
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
geom: Mapped[WKBElement] = mapped_column(
Geometry(geometry_type="POLYGON", srid=4326), nullable=False
)
bbox: Mapped[WKBElement] = mapped_column(
BBoxGeometry(geometry_type="POLYGON", srid=4326), nullable=False
)
Base.metadata.drop_all(engine, checkfirst=True)
Base.metadata.create_all(engine)
#
raw_geom = 'POLYGON ((0 0, 1 1, 2 0, 1 -1, 0 0))'
geom = WKTElement(raw_geom)
geofences = [
Geofence(
id=1,
geom=geom,
bbox=geom,
),
Geofence(
id=2,
geom=geom,
bbox=geom,
)
]
Session = sessionmaker(bind=engine, expire_on_commit=False)
with Session() as session:
# With ORM
try:
session.bulk_save_objects(geofences)
except Exception:
session.rollback()
raise
else:
session.commit()
# With core
conn = session.connection()
i = Geofence.__table__.insert()
i = i.values(id=bindparam("id"), geom=bindparam("geom"), bbox=func.ST_Envelope(func.ST_GeomFromEWKT(bindparam("geom"))))
conn.execute(i, [{"id": j.id + 10, "geom": j.geom, "bbpx": j.bbox} for j in geofences])
# Check the inserted results
res = conn.execute(select(Geofence.__table__.c.id, Geofence.__table__.c.geom.ST_AsText(), Geofence.__table__.c.bbox.ST_AsText())).all()
for j in res:
print(j)
from geoalchemy2.
Related Issues (20)
- How do I install it in an offline environment? HOT 4
- Unit tests fail on s390x (big-endian) HOT 10
- "no such table: main.idx_{table}_{col}" Exception when writing Spatialite HOT 6
- Performance improvement suggestions for load_spatialite HOT 2
- 0.14.0 broken initialization of WKTElements HOT 6
- Add type annotations
- Multipolygon with one polygon is forced to be Polygon HOT 2
- Fix MySQL dialect HOT 2
- AtttributeError: 'Nonetype' object has no attribute 'replace' HOT 7
- Add support for Mapped and mapped_column from sqlalchemy 2 HOT 3
- bigquery insert error with geometry field HOT 3
- WKBElement in postgres CASE WHEN doesn't work HOT 9
- mypy: Skipping analyzing "geoalchemy2.functions": module is installed, but missing library stubs or py.typed marker [import] HOT 7
- [Feature request] Support Python3.12 HOT 4
- [How to use] GeoAlchemy ORM with MySQL HOT 6
- [Bug report] When using MariaDB, `add` generates wrong SQL HOT 9
- using func.ST_DWithin outputs error stating "No function matches the given name and argument types." HOT 8
- Assistanze in using 3D Geometries HOT 4
- [Feature request] Add compatibility with MSSQL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geoalchemy2.