一、定义空间字段:
因为该版本支持创建Geography字段类型,因此创建空间字段不同于之前版本,采用了如下形式:
geom =Column(Geometry('POLYGON’))
二、空间函数的使用:
需要注意的是所有函数都带有ST_前缀,这也是为了与postgis写法一致.
函数的使用方法:
第一种是使用func,例如在orm中你可以:
Session.query(func.ST_Area(Lake.geom))或Session.query(Lake.geom.ST_Area()) 进行面积查询
不使用orm你可以:
select([func.ST_Area(lake_table.c.geom)])
三、支持wkt和wkb格式
例如WKTElement('POINT(5 45)’)和WKTElement('POINT(5 45)',srid=4326)对应着Point类型的Geometry,和postgis中的ST_GeomFromText效果相同。
同时支持与shapely库的空间类型互相转换,详情请查阅geoalchemy2.shape.from_shape和geoalchemy2.shape.to_shape方法
空间函数整理:(谷歌机翻加上了自己的一点调整)
函数名 返回类型
2.1 geometry访问相关
('ST_Envelope',types.Geometry,
'返回提供的geometry的外边框,双精度(float8)’)
('ST_GeometryN',无,
'如果几何是``GEOMETRYCOLLECTION``,``(MULTI)POINT``,``(MULTI)LINESTRING``,``MULTICURVE``或``(MULTI)POLYGON``,``POLYHEDRALSURFACE``,则返回基于1的第N个几何, 否则返回``None``。),
('ST_GeometryType',无,
'返回``ST_Geometry``返回值的几何类型。'),
('ST_IsValid',无,
如果``ST_Geometry``返回的空间数据有效,则返回``True``。'),
('ST_NPoints',无,
'返回Geometry数据中的节点数。'),
('ST_SRID',无,
‘返回Geometry数据的SRID数值,该数值参考spatial_ref_sys表中的定义)
('ST_X',无,
返回点的X坐标,如果不可用,则返回“无”。输入必须是一个Point类型数据),
('ST_Y',无,返回点的Y坐标,如果不可用,则返回“无”。输入必须是一个Point类型数据)
('ST_Z',无,返回点的Z坐标,如果不可用,则返回“无”。输入必须是一个Point类型数据)
2.2 geometry编辑相关
('ST_Transform',types.Geometry,
'返回一个新的几何体,其坐标转换为输入的SRID'),
2.3 geometry输出
('ST_AsBinary',无,转化成WKB),
('ST_AsEWKB',无,转化成EWKB),
('ST_AsTWKB',无,转化成TWKB),
('ST_AsGeoJSON',无,'将geometry转化成GeoJSON元素返回。'),
('ST_AsGML',无,'将geometry转化成GML版本2或3。'),
('ST_AsKML',无,
'将geometry转化为KML元素返回。多个类型。默认参数:'version = 2,default precision= 15'),
('ST_AsSVG',无,
'返回输入geometry或geography的SVG格式数据'),
('ST_AsText',无,返回输入geometry的WKT格式,没有SRID信息)
('ST_AsEWKT',无,
'返回输入geometry的EWKT格式数据,具有SRID信息。'),
2.4 空间关系和测量
('ST_Area',无,如果是polygon或multi-polygon,则返回平面面积;对于geometry类型的面积单位取决于SRID。对于geography类型面积单位为平方米),
('ST_Azimuth',无,返回pointb和pointb之间的角度的弧度值,顺时针计算,12 = 0; 3 = PI / 2; 6 = PI; 9 = 3PI / 2),
('ST_Centroid',types.Geometry,返回geometry的几何中心),
('ST_Contains',无,A是否包含B'),
('ST_ContainsProperly',A是否完全包含B,不内切),
('ST_Covers',无,A是否包含B,可以内切,
('ST_CoveredBy',没有,B是否包含A),
('ST_Crosses',无,
如果提供的几何有一些但不是全部,则返回``True``
'内部点共同点。'),
('ST_Disjoint',无,
如果几何不“空间相交”,则返回``True`` - 如果'
“他们不共享任何空间。”),
('ST_Distance',无,
'对于几何类型返回二维笛卡尔最小距离'
'(基于空间参考)在投影单位中的两个几何之间。为''
'地理类型默认返回'之间的球形最小距离'
'米的两个地理位置。'),
('ST_Distance_Sphere',无,
'返回两个lon / lat几何之间的最小距离(以米为单位)。使用'
'球形地球,半径6370986米。比...快 '
'``ST_Distance_Spheroid``,但不太准确。 PostGIS版本'
'在1.5之前只实现积分。'),
('ST_DFullyWithin',没有,
'如果所有几何都在指定的范围内,则返回``True``
'彼此的距离')
'ST_DWithin',没有,
如果空间图形在另一个空间图形指定的距离内,则返回True。单位视空间坐标系而定,geography的单位为米。默认为球面,栅格数据默认为非球面
('ST_Intersects',无,
如果geometry/geography相交,则返回true。
('ST_Length',无,
'如果它是一个LINESTRING或MULTILINESTRING,则返回几何的2d长度。几何以空间参考为单位,geography以米为单位(默认球体)'),
('ST_LineLocatePoint',无,
'返回一个介于0和1之间的浮点数,表示LineString上最接近给定Point的点的距离占LineString总长的百分比),
('ST_OrderingEquals',无,
如果给定的几何表示相同的几何,则返回``True``
'和点的方向顺序相同。'),
('ST_Overlaps',无,
如果Geometries共享空间,则返回``True``,是相同的'
'维度,但彼此不完全包含。'),
('ST_Perimeter',无,
'返回ST_Surface边界的长度测量值或'
'ST_MultiSurface几何或地理。 (多边形,多边形)。 “
'几何测量以空间参考为单位,地理是'
'米。'),
('ST_Project',types.Geography,
'返回一个从起始点投射的``POINT``使用距离'
'米和轴承(方位角)以弧度表示。'),
('ST_Relate',无,
'如果此几何与空间相关',则返回``True``
'另一个几何,通过测试内部之间的交叉点,'
'由值指定的两个几何的边界和外部'
'在intersectionMatrixPattern中。如果没有intersectionMatrixPattern'
'传入,然后返回最大的intersectionMatrixPattern'
'涉及两种几何形状。'),
('ST_Touches',无,
如果几何有至少一个共同点,'返回``True``
'但他们的内部不相交。'),
('ST_Within',没有,
'如果几何A完全在几何B'内,则返回``True``,
#
#几何处理
#
('ST_Buffer',types.Geometry,
'对于几何体:返回表示其所有点的几何体'
'与此几何的距离小于或等于距离。 “
'计算在此几何的空间参照系中。\ n \ n'
'对于地理:使用平面变换包装。介绍1.5'
'支持不同的端盖和斜接设置来控制形状。'),
('ST_Difference',types.Geometry,
'返回表示几何A的那部分的几何'
'与几何B不相交''),
('ST_Dump',types.GeometryDump,
'返回一组geometry_dump(geom,path)行,组成''
'geometry g1。'),
('ST_DumpPoints',types.GeometryDump,
'返回一组所有点的geometry_dump(geom,path)行'
'构成一个几何。'),
('ST_Intersection',types.Geometry,
'返回表示geomA和'的共享部分的几何
“geomB。地理位置实现了对几何体的转换'
'交叉点然后转换回WGS84。'),
('ST_LineMerge',types.Geometry,
'返回一个(一组)LineString(s)形成的缝合在一起'
'MULTILINESTRING的组成线工作。'),
('ST_LineSubstring',types.Geometry,
'返回一个线串作为输入的一个子串开始和'
'以总共2d长度的给定分数结束。第二和第三'
'参数是介于0和1之间的float8值。这仅适用于'
“线串。要使用连续的MULTILINESTRING,请使用'
'与ST_LineMerge结合。'
“”
'如果\'开始\'和\'结尾\'具有相同的值,这相当于'
'到ST_LineInterpolatePoint。'),
('ST_Union',types.Geometry,
'返回表示'的点集联合的几何'
'的几何形状。'),
('ST_Simplify',types.Geometry,
'使用'返回给定几何的“简化”版本
'Douglas-Peucker算法。'),
#
#Raster构造函数
#
('ST_GeomFromText',types.Geometry,
'从OGC着名文本构造一个PostGIS ST_Geometry对象'
'表示。’),
('ST_GeomFromEWKT',types.Geometry,
'从OGC Extended Well-Known构造一个PostGIS ST_Geometry对象'
'text(EWKT)表示。'),
('ST_GeomFromEWKB',types.Geometry,
'从OGC Extended Well-Known构造一个PostGIS ST_Geometry对象'
'二进制(EWKB)表示。'),
('ST_GeogFromText',types.Geography,
'从众所周知的文本中返回地理对象或扩展众所周知的'
'表示。'),
('ST_AsRaster',types.Raster,
('将PostGIS几何体转换为PostGIS栅格。','RT_ST_AsRaster')),
#
#Raster访问者
#
('ST_Height',无,
('返回栅格的高度,以像素为单位。','RT_ST_Height')),
('ST_Width',无,
('返回栅格的宽度,以像素为单位。','RT_ST_Width')),
#
#Raster Pixel Accessors和Setters
#
('ST_Value',无,
('返回给定columnx,rowy pixel或at'中给定band的值
'一个特定的几何点。乐队编号从1开始并假设为'
'如果没有指定则为1。如果``exclude_nodata_value``设置为'
'``false``,然后所有像素都包含nodata像素被认为是'
'相交并返回值。如果没有传递``exclude_nodata_value``'
'然后从栅格的元数据中读取它。','RT_ST_Value’))
翻译不动了,贴上原文:
('ST_Envelope', types.Geometry,
'Returns a geometry representing the double precision (float8) bounding'
'box of the supplied geometry.'),
('ST_GeometryN',None,
'Return the 1-based Nth geometry if the geometry is a '
'``GEOMETRYCOLLECTION``, ``(MULTI)POINT``, ``(MULTI)LINESTRING``, '
'``MULTICURVE`` or ``(MULTI)POLYGON``, ``POLYHEDRALSURFACE`` Otherwise, '
'return ``None``.'),
('ST_GeometryType',None,
'Return the geometry type of the ``ST_Geometry`` value.'),
('ST_IsValid',None,
'Returns ``True`` if the ``ST_Geometry`` is well formed.'),
('ST_NPoints',None,
'Return the number of points (vertices) in a geometry.'),
('ST_SRID',None,
'Returns the spatial reference identifier for the ``ST_Geometry`` as '
'defined in ``spatial_ref_sys`` table.'),
('ST_X',None,
'Return the X coordinate of the point, or ``None`` if not available. '
'Input must be a point.'),
('ST_Y',None,
'Return the Y coordinate of the point, or ``None`` if not available. '
'Input must be a point.'),
('ST_Z',None,
'Return the Z coordinate of the point, or ``None`` if not available. '
'Input must be a point.'),
#
# Geometry Editors
#
('ST_Transform', types.Geometry,
'Return a new geometry with its coordinates transformed to the SRID '
'referenced by the integer parameter.'),
#
# Geometry Outputs
#
('ST_AsBinary',None,
'Return the Well-Known Binary (WKB) representation of the geometry/'
'geography without SRID meta data.'),
('ST_AsEWKB',None,
'Return the Well-Known Binary (WKB) representation of the geometry/'
'geography with SRID meta data.'),
('ST_AsTWKB',None,
'Returns the geometry as TWKB, aka "Tiny Well-Known Binary"'),
('ST_AsGeoJSON',None,'Return the geometry as a GeoJSON element.'),
('ST_AsGML',None,'Return the geometry as a GML version 2 or 3 element.'),
('ST_AsKML',None,
'Return the geometry as a KML element. Several variants. Default '
'version=2, default precision=15'),
('ST_AsSVG',None,
'Returns a Geometry in SVG path data given a geometry or geography '
'object.'),
('ST_AsText',None,
'Return the Well-Known Text (WKT) representation of the geometry/'
'geography without SRID metadata.'),
('ST_AsEWKT',None,
'Return the Well-Known Text (WKT) representation of the geometry/'
'geography with SRID metadata.'),
#
# Spatial Relationships and Measurements
#
('ST_Area',None,
'Returns the area of the surface if it is a polygon or multi-polygon. '
'For ``geometry`` type area is in SRID units. For ``geography`` area is '
'in square meters.'),
('ST_Azimuth',None,
'Returns the angle in radians from the horizontal of the '
'vector defined by pointA and pointB. Angle is computed clockwise from '
'down-to-up: on the clock: 12=0; 3=PI/2; 6=PI; 9=3PI/2.'),
('ST_Centroid', types.Geometry,
'Returns the geometric center of a geometry.'),
('ST_Contains',None,
'Returns ``True`` if and only if no points of B lie in the exterior of '
'A, and at least one point of the interior of B lies in the interior '
'of A.'),
('ST_ContainsProperly',None,
'Returns ``True`` if B intersects the interior of A but not the boundary '
'(or exterior). A does not contain properly itself, but does contain '
'itself.'),
('ST_Covers',None,
'Returns ``True`` if no point in Geometry B is outside Geometry A'),
('ST_CoveredBy',None,
'Returns ``True`` if no point in Geometry/Geography A is outside Geometry'
'/Geography B'),
('ST_Crosses',None,
'Returns ``True`` if the supplied geometries have some, but not all, '
'interior points in common.'),
('ST_Disjoint',None,
'Returns ``True`` if the Geometries do not "spatially intersect" - if '
'they do not share any space together.'),
('ST_Distance',None,
'For geometry type Returns the 2-dimensional cartesian minimum distance '
'(based on spatial ref) between two geometries in projected units. For '
'geography type defaults to return spheroidal minimum distance between '
'two geographies in meters.'),
('ST_Distance_Sphere',None,
'Returns minimum distance in meters between two lon/lat geometries. Uses '
'a spherical earth and radius of 6370986 meters. Faster than '
'``ST_Distance_Spheroid``, but less accurate. PostGIS versions '
'prior to 1.5 only implemented for points.'),
('ST_DFullyWithin',None,
'Returns ``True`` if all of the geometries are within the specified '
'distance of one another'),
('ST_DWithin',None,
'Returns ``True`` if the geometries are within the specified distance of '
'one another. For geometry units are in those of spatial reference and '
'for geography units are in meters and measurement is defaulted to '
'``use_spheroid=True`` (measure around spheroid), for faster check, '
'``use_spheroid=False`` to measure along sphere.'),
('ST_Equals',None,
'Returns ``True`` if the given geometries represent the same geometry. '
'Directionality is ignored.'),
('ST_Intersects',None,
'Returns ``True`` if the Geometries/Geography "spatially intersect in '
'2D" - (share any portion of space) and ``False`` if they don\'t (they '
'are Disjoint). For geography -- tolerance is 0.00001 meters (so any '
'points that close are considered to intersect)'),
('ST_Length',None,
'Returns the 2d length of the geometry if it is a linestring or '
'multilinestring. geometry are in units of spatial reference and '
'geography are in meters (default spheroid)'),
('ST_LineLocatePoint',None,
'Returns a float between 0 and 1 representing the location of the '
'closest point on LineString to the given Point, as a fraction of '
'total 2d line length.'),
('ST_OrderingEquals',None,
'Returns ``True`` if the given geometries represent the same geometry '
'and points are in the same directional order.'),
('ST_Overlaps',None,
'Returns ``True`` if the Geometries share space, are of the same '
'dimension, but are not completely contained by each other.'),
('ST_Perimeter',None,
'Return the length measurement of the boundary of an ST_Surface or '
'ST_MultiSurface geometry or geography. (Polygon, Multipolygon). '
'geometry measurement is in units of spatial reference and geography is '
'in meters.'),
('ST_Project', types.Geography,
'Returns a ``POINT`` projected from a start point using a distance in '
'meters and bearing (azimuth) in radians.'),
('ST_Relate',None,
'Returns ``True`` if this Geometry is spatially related to '
'anotherGeometry, by testing for intersections between the Interior, '
'Boundary and Exterior of the two geometries as specified by the values '
'in the intersectionMatrixPattern. If no intersectionMatrixPattern is '
'passed in, then returns the maximum intersectionMatrixPattern that '
'relates the 2 geometries.'),
('ST_Touches',None,
'Returns ``True`` if the geometries have at least one point in common, '
'but their interiors do not intersect.'),
('ST_Within',None,
'Returns ``True`` if the geometry A is completely inside geometry B'),
#
# Geometry Processing
#
('ST_Buffer', types.Geometry,
'For geometry: Returns a geometry that represents all points whose '
'distance from this Geometry is less than or equal to distance. '
'Calculations are in the Spatial Reference System of this Geometry.\n\n'
'For geography: Uses a planar transform wrapper. Introduced in 1.5 '
'support for different end cap and mitre settings to control shape.'),
('ST_Difference', types.Geometry,
'Returns a geometry that represents that part of geometry A that does '
'not intersect with geometry B.'),
('ST_Dump', types.GeometryDump,
'Returns a set of geometry_dump (geom,path) rows, that make up a '
'geometry g1.'),
('ST_DumpPoints', types.GeometryDump,
'Returns a set of geometry_dump (geom,path) rows of all points that '
'make up a geometry.'),
('ST_Intersection', types.Geometry,
'Returns a geometry that represents the shared portion of geomA and '
'geomB. The geography implementation does a transform to geometry to do '
'the intersection and then transform back to WGS84.'),
('ST_LineMerge', types.Geometry,
'Returns a (set of) LineString(s) formed by sewing together the '
'constituent line work of a MULTILINESTRING.'),
('ST_LineSubstring', types.Geometry,
'Return a linestring being a substring of the input one starting and '
'ending at the given fractions of total 2d length. Second and third '
'arguments are float8 values between 0 and 1. This only works with '
'LINESTRINGs. To use with contiguous MULTILINESTRINGs use in '
'conjunction with ST_LineMerge.'
''
'If\'start\'and\'end\'have the same value this is equivalent '
'to ST_LineInterpolatePoint.'),
('ST_Union', types.Geometry,
'Returns a geometry that represents the point set union of the '
'Geometries.'),
('ST_Simplify', types.Geometry,
'Returns a "simplified" version of the given geometry using the '
'Douglas-Peucker algorithm.'),
#
# Raster Constructors
#
('ST_GeomFromText', types.Geometry,
'Constructs a PostGIS ST_Geometry object from the OGC Well-Known text '
'representation.'),
('ST_GeomFromEWKT', types.Geometry,
'Constructs a PostGIS ST_Geometry object from the OGC Extended Well-Known '
'text (EWKT) representation.'),
('ST_GeomFromEWKB', types.Geometry,
'Constructs a PostGIS ST_Geometry object from the OGC Extended Well-Known '
'binary (EWKB) representation.'),
('ST_GeogFromText', types.Geography,
'Returns a geography object from the well-known text or extended well-known '
'representation.'),
('ST_AsRaster', types.Raster,
('Converts a PostGIS geometry to a PostGIS raster.','RT_ST_AsRaster')),
#
# Raster Accessors
#
('ST_Height',None,
('Returns the height of the raster in pixels.','RT_ST_Height')),
('ST_Width',None,
('Returns the width of the raster in pixels.','RT_ST_Width')),
#
# Raster Pixel Accessors and Setters
#
('ST_Value',None,
('Returns the value of a given band in a given columnx, rowy pixel or at '
'a particular geometric point. Band numbers start at 1 and assumed to '
'be 1 if not specified. If ``exclude_nodata_value`` is set to '
'``false``, then all pixels include nodata pixels are considered to '
'intersect and return value. If ``exclude_nodata_value`` is not passed '
'in then reads it from metadata of raster.','RT_ST_Value'))