本文整理匯總了Python中geoalchemy2.Geometry方法的典型用法代碼示例。如果您正苦於以下問題:Python geoalchemy2.Geometry方法的具體用法?Python geoalchemy2.Geometry怎麽用?Python geoalchemy2.Geometry使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類geoalchemy2
的用法示例。
在下文中一共展示了geoalchemy2.Geometry方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ga_geometry
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def ga_geometry(_, gatype, nullable=True):
t = gatype.geometry_type
if t == 'POINT':
return dt.Point(nullable=nullable)
if t == 'LINESTRING':
return dt.LineString(nullable=nullable)
if t == 'POLYGON':
return dt.Polygon(nullable=nullable)
if t == 'MULTILINESTRING':
return dt.MultiLineString(nullable=nullable)
if t == 'MULTIPOINT':
return dt.MultiPoint(nullable=nullable)
if t == 'MULTIPOLYGON':
return dt.MultiPolygon(nullable=nullable)
if t == 'GEOMETRY':
return dt.Geometry(nullable=nullable)
else:
raise ValueError("Unrecognized geometry type: {}".format(t))
示例2: oql_for_chunk
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def oql_for_chunk(self, chunk, include_self=False, skip=None):
skip = set(skip or [])
q = self.items.filter(cast(Item.location, Geometry).contained(envelope(chunk)))
tags = set()
for item in q:
tags |= set(item.tags)
tags.difference_update(skip_tags)
tags.difference_update(skip)
tags = matcher.simplify_tags(tags)
if not(tags):
print('no tags, skipping')
return
ymin, ymax, xmin, xmax = chunk
bbox = '{:f},{:f},{:f},{:f}'.format(ymin, xmin, ymax, xmax)
oql = overpass.oql_for_area(self.overpass_type,
self.osm_id,
tags,
bbox, None,
include_self=include_self)
return oql
示例3: setup_spatial_table
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def setup_spatial_table(package_extent_class, db_srid=None):
if legacy_geoalchemy:
package_extent_table = Table(
'package_extent', meta.metadata,
Column('package_id', types.UnicodeText, primary_key=True),
GeometryExtensionColumn('the_geom', Geometry(2, srid=db_srid))
)
meta.mapper(
package_extent_class,
package_extent_table,
properties={'the_geom':
GeometryColumn(package_extent_table.c.the_geom,
comparator=PGComparator)}
)
GeometryDDL(package_extent_table)
else:
# PostGIS 1.5 requires management=True when defining the Geometry
# field
management = (postgis_version()[:1] == '1')
package_extent_table = Table(
'package_extent', meta.metadata,
Column('package_id', types.UnicodeText, primary_key=True),
Column('the_geom', Geometry('GEOMETRY', srid=db_srid,
management=management)),
)
meta.mapper(package_extent_class, package_extent_table)
return package_extent_table
示例4: add_geometry_column
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
cls.geom = Column(Geometry(geometry_type='POINT', srid=config.SRID))
示例5: add_geometry_column
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
from geoalchemy2 import Geometry
cls.geom = Column(Geometry(geometry_type='POINT', srid=config.SRID))
示例6: add_geometry_column
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
from geoalchemy2 import Geometry
cls.geom = deferred(Column(Geometry('MULTILINESTRING')))
示例7: add_geometry_column
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def add_geometry_column(cls):
if not hasattr(cls, 'geom'):
cls.geom = deferred(Column(Geometry(geometry_type='LINESTRING', srid=config.SRID)))
示例8: show_polygons
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def show_polygons(place_identifier):
place = get_place(place_identifier)
num = 0
for chunk in place.polygon_chunk(size=64):
num += 1
print(chunk)
print()
print(num)
return
num = '(-?[0-9.]+)'
re_box = re.compile(f'^BOX\({num} {num},{num} {num}\)$')
# select ST_Dump(geom::geometry) as poly from place where osm_id=1543125
stmt = (database.session.query(func.ST_Dump(Place.geom.cast(Geometry())).label('x'))
.filter_by(place_id=place.place_id)
.subquery())
q = database.session.query(stmt.c.x.path[1],
func.ST_Area(stmt.c.x.geom.cast(Geography)) / (1000 * 1000),
func.Box2D(stmt.c.x.geom))
print(q)
for num, area, box2d in q:
# west, south, east, north
# BOX(135.8536855 20.2145811,136.3224209 20.6291059)
size = wikidata_chunk_size(area)
west, south, east, north = map(float, re_box.match(box2d).groups())
bbox = (south, north, west, east)
# print((num, area, size, box2d))
for chunk in chunk_n(bbox, size):
print(chunk)
示例9: polygon_chunk
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def polygon_chunk(self, size=64):
stmt = (session.query(func.ST_Dump(Place.geom.cast(Geometry())).label('x'))
.filter_by(place_id=self.place_id)
.subquery())
q = session.query(stmt.c.x.path[1],
func.ST_Area(stmt.c.x.geom.cast(Geography)) / (1000 * 1000),
func.Box2D(stmt.c.x.geom))
for num, area, box2d in q:
chunk_size = utils.calc_chunk_size(area, size=size)
west, south, east, north = map(float, re_box.match(box2d).groups())
for chunk in bbox_chunk((south, north, west, east), chunk_size):
yield chunk
示例10: geometry_updater
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def geometry_updater(column, table, columns):
"""
Updater for a geometry column.
:param column: Geometry column
:type column: GeometryColumn
:param columns: Existing column names in the database for the given table.
:type columns: list
"""
geom_type = column.geometry_type()
return _update_col(column, table, Geometry(geometry_type=geom_type,
srid=column.srid),
columns)
示例11: srid_equality
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def srid_equality(tables):
"""
Check whether there is only one projection in list of tables.
Parameters
----------
tables: iterable
List of table ORM classes to inspect geometry columns.
Returns
-------
unique: boolean
"""
# Iterate over all columns to build set of SRIDs.
srids = set()
for table in tables:
for c in table.__table__.columns:
if isinstance(c.type, Geometry):
# Column is geometry column.
srids.add(c.type.srid)
# Projection is unique if set has single SRID.
assert len(srids) > 0
if len(srids) == 1:
return True
else:
return False
示例12: _to_sqla_type
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def _to_sqla_type(itype, type_map=None):
if type_map is None:
type_map = _ibis_type_to_sqla
if isinstance(itype, dt.Decimal):
return sa.types.NUMERIC(itype.precision, itype.scale)
elif isinstance(itype, dt.Date):
return sa.Date()
elif isinstance(itype, dt.Timestamp):
# SQLAlchemy DateTimes do not store the timezone, just whether the db
# supports timezones.
return sa.TIMESTAMP(bool(itype.timezone))
elif isinstance(itype, dt.Array):
ibis_type = itype.value_type
if not isinstance(ibis_type, (dt.Primitive, dt.String)):
raise TypeError(
'Type {} is not a primitive type or string type'.format(
ibis_type
)
)
return sa.ARRAY(_to_sqla_type(ibis_type, type_map=type_map))
elif geospatial_supported and isinstance(itype, dt.GeoSpatial):
if itype.geotype == 'geometry':
return ga.Geometry
elif itype.geotype == 'geography':
return ga.Geography
else:
return ga.types._GISType
else:
return type_map[type(itype)]
示例13: trim
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def trim(target_col, trim_col):
"""
Trim target geometry by removing intersection with a trim column.
Parameters
----------
target_col : sqlalchemy.orm.attributes.InstrumentedAttribute
Column ORM object to trim.
trim_col : sqlalchemy.orm.attributes.InstrumentedAttribute
Column ORM object to trim target column with.
Returns
-------
None
"""
# TODO: Aggregate multiple rows in trim_col.
# Needs testing to make sure that ST_Difference can handle MultiPolygons
# without data loss.
with db.session() as sess:
data_type = target_col.property.columns[0].type
geom_type = data_type.geometry_type
if geom_type.lower() == "multipolygon":
# ST_Difference outputs Polygon, not MultiPolygon.
# Temporarily change the geometry data type to generic Geometry.
column_name = target_col.name
table_name = target_col.parent.tables[0].name
schema_name = target_col.parent.tables[0].schema
srid = data_type.srid
sess.execute("""
ALTER TABLE {schema}.{table} ALTER COLUMN {column}
SET DATA TYPE geometry(Geometry, {srid});
""".format(
schema=schema_name, table=table_name, column=column_name,
srid=srid)
)
# Update column value with ST_Difference if ST_Intersects.
table = target_col.parent
sess.query(table).filter(
target_col.ST_Intersects(trim_col)
).update(
{target_col: target_col.ST_Difference(trim_col)},
synchronize_session=False
)
if geom_type.lower() == "multipolygon":
# Coerce the geometry data type back to MultiPolygon.
sess.execute("""
ALTER TABLE {schema}.{table} ALTER COLUMN {column}
SET DATA TYPE geometry(MultiPolygon, {srid})
USING ST_Multi(geom);
""".format(
schema=schema_name, table=table_name, column=column_name,
srid=srid)
)
示例14: conform_srids
# 需要導入模塊: import geoalchemy2 [as 別名]
# 或者: from geoalchemy2 import Geometry [as 別名]
def conform_srids(srid, schema=None, fix=False):
"""
Reproject all non-conforming geometry columns into the specified SRID.
Parameters
----------
srid : int
Spatial Reference System Identifier (SRID).
schema : schema class, optional
If schema is specified, only SRIDs within the specified schema
are conformed.
fix : bool, optional
Whether to report and attempt to fix invalid geometries.
Returns
-------
None
"""
# Iterate over all columns. Reproject geometry columns with SRIDs
# that differ from project SRID.
for schema_name, schema_obj in db.tables.__dict__.items():
if not schema_name.startswith('_'):
if not schema or schema_name == schema.__name__:
for table_name, table in schema_obj.__dict__.items():
if not table_name.startswith('_'):
for c in table.__table__.columns:
if isinstance(c.type, Geometry):
# Fix geometry if asked to do so.
if fix:
if c.name == 'geom':
invalid_df = geom_invalid(table)
if not invalid_df.empty:
logger.warn(invalid_df)
validate(table=table)
else:
validate(column=getattr(table,
c.name))
# Reproject if SRID differs from project SRID.
current_srid = c.type.srid
if srid != current_srid:
column = getattr(table, c.name)
reproject(srid, table, column)