Is there a convenient way to create spatial index with Sqlalchemy or Geoalchemy?

被刻印的时光 ゝ 提交于 2019-12-06 09:56:56

问题


Yesterday someone told me that Geoalchemy was a good choice to support spatial extensions if I want to use Sqlalchemy. It really did good. Thanks for him a lot.

But I have not found a convenient way to create spatial index with Geoalchemy. Must I run this SQL directly?

CREATE SPATIAL INDEX sp_index ON my_class (
geom_col
);

Is there a convenient way to do it without directly writing the SQL?

Thanks!


回答1:


When creating a table with table.create() GeoAlchemy will automatically create an index for the geometry column.

Also, SQLAlchemy Index instances have a create method. See the "Indexes" section in http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#metadata-constraints. I'm not sure if you'll be able to create a spatial index in MySQL with that but it's worth a try.




回答2:


With GeoAlchemy, there are 2 steps you should take to create Spatial Index on the geometry column

  • add attr 'spatial_index' to the geometry column
  • Call the GeometryDDL extension

Example:

# note that spatial_index will set the column to not null 
class Building(Base):
    geom = GeometryColumn(Point(2, spatial_index=True))

GeometryDDL(Building.__table__)


来源:https://stackoverflow.com/questions/10462455/is-there-a-convenient-way-to-create-spatial-index-with-sqlalchemy-or-geoalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!