@@ -284,6 +284,59 @@ The resulting object will look like this::
284284 change, the `UPDATE `_ statement sent to CrateDB will include all of the
285285 ``ObjectArray `` data.
286286
287+ .. _geopoint :
288+ .. _geoshape :
289+
290+ ``Geopoint `` and ``Geoshape ``
291+ .............................
292+
293+ The CrateDB SQLAlchemy dialect provides two geospatial types:
294+
295+ - ``Geopoint ``, which represents a longitude and latitude coordinate
296+ - ``Geoshape ``, which is used to store geometric `GeoJSON geometry objects `_
297+
298+ To use these types, you can create columns, like so::
299+
300+ >>> class City(Base):
301+ ...
302+ ... __tablename__ = 'cities'
303+ ... name = sa.Column(sa.String, primary_key=True)
304+ ... coordinate = sa.Column(types.Geopoint)
305+ ... area = sa.Column(types.Geoshape)
306+
307+ There are multiple ways of creating a geopoint. Firstly, you can define it as
308+ a tuple of ``(longitude, latitude) ``::
309+
310+ >>> point = (139.76, 35.68)
311+
312+ Secondly, you can define it as a geojson ``Point `` object::
313+
314+ >>> from geojson import Point
315+ >>> point = Point(coordinates=(139.76, 35.68))
316+
317+ To create a geoshape, you can use a geojson shape object, such as a ``Polygon ``::
318+
319+ >>> from geojson import Point, Polygon
320+ >>> area = Polygon(
321+ ... [
322+ ... [
323+ ... (139.806, 35.515),
324+ ... (139.919, 35.703),
325+ ... (139.768, 35.817),
326+ ... (139.575, 35.760),
327+ ... (139.584, 35.619),
328+ ... (139.806, 35.515),
329+ ... ]
330+ ... ]
331+ ... )
332+
333+ You can then set the values of the ``Geopoint `` and ``Geoshape `` columns::
334+
335+ >>> tokyo = City(name="Tokyo", coordinate=point, area=area)
336+ >>> session.add(tokyo)
337+ >>> session.commit()
338+
339+
287340Querying
288341========
289342
@@ -535,3 +588,4 @@ column on the ``Character`` class.
535588.. _score : https://crate.io/docs/crate/reference/en/latest/general/dql/fulltext.html#usage
536589.. _working with tables : http://docs.sqlalchemy.org/en/latest/core/metadata.html
537590.. _UUIDs : https://docs.python.org/3/library/uuid.html
591+ .. _geojson geometry objects : https://tools.ietf.org/html/rfc7946#section-3.1
0 commit comments