Chapter 4 Data Management
译:章节4 数据管理
4.1 Spatial Data Model
译:4.1 空间数据模型
4.1.1 OGC Geometry
译:4.1.1 OGC标准几何要素
The Open Geospatial Consortium (OGC) developed the Simple Features Access standard (SFA) to provide a model for geospatial data. It defines the fundamental spatial type of Geometry, along with operations which manipulate and transform geometry values to perform spatial analysis tasks. PostGIS implements the OGC Geometry model as the PostgreSQL data types geometry and geography.
译:开放地理空间联盟(Open Geospatial Consortium,OGC)为给地理空间数据提供模型发布了简单要素访问标准(Sim-ple Features Access,SFA)。它定义了 Geometry 基本的空间类型,以及操作和转换几何值以执行空间分析任务的操 作。PostGIS 作为 PostgreSQL 数据几何和地理类型实现 OGC 几何模型。
Geometry is an abstract type. Geometry values belong to one of its concrete subtypes which represent various kinds and dimensions of geometric shapes. These include the atomic types Point, LineString, LinearRing and Polygon, and the collection types MultiPoint, MultiLineString, MultiPolygon and GeometryCollection. The Simple Features Access - Part 1: Common architecture v1.2.1 adds subtypes for the structures PolyhedralSurface, Triangle and TIN.
译:Geometry 是一个抽象类型。Geometry 的值是几何要素的属性之一,它描述了几何要素的类型和大小。其中包括原子类型 Point、LineString、LinearRing 和 Polygon,以及集合类 MultiPoint、MultiLineString、MultiPolygon 和 GeometryCo-llection。简单要素访问标准第一部分通用版 v1.2.1为结构 PolyhedralSurface、 Triangle、TIN 添加了子类型。
Geometry models shapes in the 2-dimensional Cartesian plane. The PolyhedralSurface, Triangle, and TIN types can also repre- sent shapes in 3-dimensional space. The size and location of shapes are specified by their coordinates. Each coordinate has a X and Y ordinate value determining its location in the plane. Shapes are constructed from points or line segments, with points specified by a single coordinate, and line segments by two coordinates.
译:几何要素可以表示二维笛卡尔平面中的形状,PolyhedralSurface、 Triangle 和 TIN 类型可以表示三维空间中的形状。形状的大小和位置由其坐标指定。每个坐标都有一个X和Y坐标值,用于确定其在平面中的位置。形状由点或线段构成(点由一个坐标指定,线段由两个坐标指定)。
Coordinates may contain optional Z and M ordinate values. The Z ordinate is often used to represent elevation. The M ordinate contains a measure value, which may represent time or distance. If Z or M values are present in a geometry value, they must be defined for each point in the geometry. If a geometry has Z or M ordinates the coordinate dimension is 3D; if it has both Z and M the coordinate dimension is 4D.
译:坐标可以包含可选的Z和M坐标值。Z坐标通常被用来表示高程,M坐标则包含一个代表时间或者距离的预测值。如果在一个几何要素中存在Z或M值,则必须为几何要素中的每个点的Z或M值赋值。如果几何要素存在Z或M值,则坐标维度为3;如果几何要素同时具有Z值和M值,则坐标维度为4。
Geometry values are associated with a spatial reference system indicating the coordinate system in which it is embedded. The spatial reference system is identified by the geometry SRID number. The units of the X and Y axes are determined by the spatial reference system. In planar reference systems the X and Y coordinates typically represent easting and northing, while in geodetic systems they represent longitude and latitude. SRID 0 represents an infinite Cartesian plane with no units assigned to its axes. See Section 4.5.
译:几何要素值与一个表名它嵌入的坐标系统相关联。空间参考系统由几何要素 SRID 编号唯一标识。X轴和Y轴由空间参考系统确定。在平面参考系统中,X坐标和Y坐标通常表示东和北;而在大地测量系统中,它们表示经度和纬度。SRID 0表示一个无限的轴上没有单位的笛卡尔平面。参见章节4.5。
The geometry dimension is a property of geometry types. Point types have dimension 0, linear types have dimension 1, and polygonal types have dimension 2. Collections have the dimension of the maximum element dimension.
译:几何要素的维数是几何要素类型的特性。点是0维,线是1维,面类型是2维。集合类型的维数为其中包含的类型的最大维数。
A geometry value may be empty. Empty values contain no vertices (for atomic geometry types) or no elements (for collections).
译:一个几何要素值可以为空。空值包含无顶点(原子几何要素类型)或无几何元素(集合类型)。
An important property of geometry values is their spatial extent or bounding box, which the OGC model calls envelope. This is the 2 or 3-dimensional box which encloses the coordinates of a geometry. It is an efficient way to represent a geometry’s extent in coordinate space and to check whether two geometries interact.
译:空间范围(extent)或包围盒(bounding box)是几何要素很重要的特性,在OGC标准模型中叫包络(envelope)。它是包含几何要素坐标的闭合的2维或3维包围盒。利用它是表示几何要素坐标空间范围和检测两个几何要素是否相交的有效方法。
The geometry model allows evaluating topological spatial relationships as described in Section 5.1.1. To support this the concepts of interior, boundary and exterior are defined for each geometry type. Geometries are topologically closed, so they always contain their boundary. The boundary is a geometry of dimension one less than that of the geometry itself.
译:几何要素模型允许估计计算拓扑空间关系(参见章节5.1.1),为了支持这一点,为每种几何类型定义了内部(interior)、边界(boundary)和外部(exterior)的概念。几何要素在拓扑上是封闭的,因此它们总是包含边界,边界 是一个维度比几何要素自身小1的几何要素。
The OGC geometry model defines validity rules for each geometry type. These rules ensure that geometry values represents realistic situations (e.g. it is possible to specify a polygon with a hole lying outside the shell, but this makes no sense geometrically and is thus invalid). PostGIS also allows storing and manipulating invalid geometry values. This allows detecting and fixing them if needed. See Section 4.4.
译:OGC 标准几何要素模型为每种几何类型定义了有效性规则。这些规则确保几何要素值所代表的真实情况(例如,可以 指定一个多边形,其中一个孔位于壳外,但这在几何上没有意义,无效)。PostGIS 还允许存储和操作无效的 几何 值。这允许在需要时检测和固定它们。参见章节4.4。
4.1.1.1 Point
译:4.1.1.1 点
A Point is a 0-dimensional geometry that represents a single location in coordinate space.
POINT (1 2)
POINT Z (1 2 3)
POINT ZM (1 2 3 4)
实践:借助sql与postgresql数据库实践如下:
说明:
①POINT 本身是xy二维
②POINT Z 是xyz三维
③POINT ZM是xyz+m四维,M为measure,测量值,多用于计算。
④sql中 st_astext为postgis函数,将空间要素转为这种字符串描述;geom为空间要素列代码名称,根据自己要素表中定义。
4.1.1.2 LineString
译:4.1.1.2 线
A LineString is a 1-dimensional line formed by a contiguous sequence of line segments. Each line segment is defined by two points, with the end point of one segment forming the start point of the next segment. An OGC-valid LineString has either zero or two or more points, but PostGIS also allows single-point LineStrings. LineStrings may cross themselves (self-intersect). A LineString is closed if the start and end points are the same. A LineString is simple if it does not self-intersect.
译:LineString 是由连续的线段形成的一维线。每个线段由两个点定义,其中一个线段的终点构成下一个线段起点。OGC标准中有效的 LineString 具有零个或两个或多个点,但 PostGIS 也允许单点的 LineString。LineStrings 可以穿过他们自己(自相交)。如果起点和终点相同,则 LineString 是闭合的。如果 LineString 不自相交,则它是简单的。
LINESTRING (1 2, 3 4, 5 6)
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.3 LineRing
译:4.1.1.3 线性环
A LineString is a 1-dimensional line formed by a contiguous sequence of line segments. Each line segment is defined by two points, with the end point of one segment forming the start point of the next segment. An OGC-valid LineString has either zero or two or more points, but PostGIS also allows single-point LineStrings. LineStrings may cross themselves (self-intersect). A LineString is closed if the start and end points are the same. A LineString is simple if it does not self-intersect.
译:LinearRing 是一个既封闭又简单的字符串。第一点和最后一点必须相等,且直线不得自相交。
LINESTRING (1 2, 3 4, 5 6)
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.4 Polygon
译:4.1.1.4 多边形
A Polygon is a 2-dimensional planar region, delimited by an exterior boundary (the shell) and zero or more interior boundaries (holes). Each boundary is a LinearRing
译:Polygon 是一个由外部边界(壳)和零个或多个内部边界(孔)界定2维平面区域。每个边界都是线性环。
POLYGON ((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0))
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.5 MultiPoint
译:4.1.1.5 多点
A MultiPoint is a collection of Points.
译:MultiPoint 是点的集合。
MULTIPOINT ( (0 0), (1 2) )
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.6 MultiLineString
译:4.1.1.6 多线
A MultiLineString is a collection of LineStrings. A MultiLineString is closed if each of its elements is closed.
译:MultiLineString 是线的集合。如果 MultiLineString 的每个元素都是闭合的,则它是闭合的。
MULTILINESTRING ( (0 0,1 1,1 2), (2 3,3 2,5 4) )
实践:借助sql与postgresql数据库实践如下:
4.1.1.7 MultiPolygon
译:4.1.1.7 多多边形
A MultiPolygon is a collection of non-overlapping, non-adjacent Polygons. Polygons in the collection may touch only at a finite number of point
译:MultiPolygon 是非重叠、非相邻多边形的集合。集合中的多边形只能接触有限数量的点。
MULTIPOLYGON (((1 5, 5 5, 5 1, 1 1, 1 5)), ((6 5, 9 1, 6 1, 6 5)))
实践:借助sql与postgresql数据库实践如下:
4.1.1.8 GeometryCollection
译:4.1.1.8 混合几何类型
A GeometryCollection is a heterogeneous (mixed) collection of geometries.
译:GeometryCollection 是由各种各样的几何图形组成的集合。
GEOMETRYCOLLECTION ( POINT(2 3), LINESTRING(2 3, 3 4))
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.9 PolyhedralSurface
译:4.1.1.9 多面体曲面
A PolyhedralSurface is a contiguous collection of patches or facets which share some edges. Each patch is a planar Polygon. If the Polygon coordinates have Z ordinates then the surface is 3-dimensional.
译:多面体曲面是共享一些边的斑块或刻面的连续集合。每个面片都是一个平面多边形。如果多边形坐标具有 Z 纵坐标,则该曲面是三维的。
POLYHEDRALSURFACE Z (
((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),
((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)),
((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.10 Triangle
译:4.1.1.10 三角形
A Triangle is a polygon defined by three distinct non-collinear vertices. Because a Triangle is a polygon it is specified by four coordinates, with the first and fourth being equal.
译:三角形是由三个不同的非共线顶点定义的多边形。三角形是一个由第一个和第四个坐标相等的四个坐标指定的多边形。
TRIANGLE ((0 0, 0 9, 9 0, 0 0))
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.1.10 TIN
译:4.1.1.10 不规则三角网
A TIN is a collection of non-overlapping Triangles representing a Triangulated Irregular Network
译:TIN 是代表不规则三角形的非重叠三角形的集合。
TIN Z ( ((0 0 0, 0 0 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 0 0 0)) )
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.2 SQL/MM Part 3 - Curves
译:4.1.2 SQL/MM 第3部分-曲线
The ISO/IEC 13249-3 SQL Multimedia - Spatial standard (SQL/MM) extends the OGC SFA to define Geometry subtypes containing curves with circular arcs. The SQL/MM types support 3DM, 3DZ and 4D coordinates.
译:ISO/IEC 13249-3 SQL 多媒体-空间标准(SQL/MM)扩展了OGC SFA来定义包含圆弧曲线的几何图形子类型。SQL/MM类型支持3维M、3维Z和4D维坐标。
Note/笔记
All floating point comparisons within the SQL-MM implementation are performed to a specified tolerance, currently 1E8.
译:SQL-MM实现中的所有浮点比较都是按照指定的公差(当前为1E8)执行的。
4.1.2.1 CircularString
译:4.1.2.1 圆
CircularString is the basic curve type, similar to a LineString in the linear world. A single arc segment is specified by three points: the start and end points (first and third) and some other point on the arc. To specify a closed circle the start and end points are the same and the middle point is the opposite point on the circle diameter (which is the center of the arc). In a sequence of arcs the end point of the previous arc is the start point of the next arc, just like the segments of a LineString. This means that a CircularString must have an odd number of points greater than 1.
译:圆是类似于线性类型中线的基本曲线类型。单个圆弧由三个点指定:起点和终点(第一个和第三个)和圆弧上的其他点。要指定闭合圆,起点和终点相同,中点是圆直径上的对应点(即圆弧的中心)。在一系列圆弧中,上一条圆弧的终点是下一条圆弧的起点,就像LineString的线段一样。这意味着圆必须有奇数个大于1的点。
CIRCULARSTRING(0 0, 1 1, 1 0)
CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0)
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.2.2 CompoundCurve
译:4.1.2.2 复合曲线
A CompoundCurve is a single continuous curve that may contain both circular arc segments and linear segments. That means that in addition to having well-formed components, the end point of every component (except the last) must be coincident with the start point of the following component.
译:复合曲线是一条连续的曲线,可以包含圆弧段和直线段。这意味着除了具有成型良好的组件外,每个组件(除了最后一个)的终点都必须是下一个组件的起点。
COMPOUNDCURVE( CIRCULARSTRING(0 0, 1 1, 1 0),(1 0, 0 1))
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.2.3 CurvePolygon
译:4.1.2.3 曲线多边形
A CurvePolygon is like a polygon, with an outer ring and zero or more inner rings. The difference is that a ring can be a CircularString or CompoundCurve as well as a LineString.
译:曲线多边形就像一个多边形,有一个外环和零个或多个内环。不同之处在于,环可以是圆、复合曲线、线。
As of PostGIS 1.4 PostGIS supports compound curves in a curve polygon.
译:自PostGIS 1.4起,PostGIS支持曲线多边形中的复合曲线。
CURVEPOLYGON(
CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0),
(1 1, 3 3, 3 1, 1 1) )
实践1:借助sql与postgresql数据库实践如下:
//TODO
Example: A CurvePolygon with the shell defined by a CompoundCurve containing a CircularString and a LineString, and a hole defined by a CircularString。
译:示例:曲线多边形,其壳由包含圆和线的复合曲线定义,孔由圆定义。
CURVEPOLYGON(
COMPOUNDCURVE( CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3),
(4 3, 4 5, 1 4, 0 0)),
CIRCULARSTRING(1.7 1, 1.4 0.4, 1.6 0.4, 1.6 0.5, 1.7 1) )
实践2:借助sql与postgresql数据库实践如下:
//TODO
4.1.2.4 MultiCurve
译:4.1.2.4 多曲线
A MultiCurve is a collection of curves which can include LineStrings, CircularStrings or CompoundCurves.
译:多曲线是一个包含线、圆、复合曲线的曲线集合。
MULTICURVE( (0 0, 5 5), CIRCULARSTRING(4 0, 4 4, 8 4))
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.2.5 MultiSurface
译:4.1.2.5 多曲面
A MultiSurface is a collection of surfaces, which can be (linear) Polygons or CurvePolygons.
译:多曲面是可以包含(线性)多边形或曲线多边形曲面的集合。
CIRCULARSTRING(0 0, 1 1, 1 0)
CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0)
实践:借助sql与postgresql数据库实践如下:
//TODO
4.1.3 WKT and WKB
译:4.1.3 已知文本/已知二进制
The OGC SFA specification defines two formats for representing geometry values for external use: Well-Known Text (WKT) and Well-Known Binary (WKB). Both WKT and WKB include information about the type of the object and the coordinates which define it.
译:OGC SFA规范定义了两种用于表示外部使用的几何值的格式:WKT和WKB。WKT和WKB都包括关于对象类型和定义对象的坐标的信息。
Well-Known Text (WKT) provides a standard textual representation of spatial data. Examples of WKT representations of spatial objects are:
译:WKT提供了空间数据的标准文本表示。空间对象的WKT表示示例如下:
• POINT(0 0)
• POINT Z (0 0 0)
• POINT ZM (0 0 0 0)
• POINT EMPTY
• LINESTRING(0 0,1 1,1 2)
• LINESTRING EMPTY
• POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
• MULTIPOINT((0 0),(1 2))
• MULTIPOINT Z ((0 0 0),(1 2 3))
• MULTIPOINT EMPTY
• MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
• MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))
• GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))
• GEOMETRYCOLLECTION EMPTY
Input and output of WKT is provided by the functions ST_AsText and ST_GeomFromText:
译:WKT的输入和输出由函数ST_AsText和ST_GeomFromText提供:
text WKT = ST_AsText(geometry);
geometry = ST_GeomFromText(text WKT, SRID);
For example, a statement to create and insert a spatial object from WKT and a SRID is:
译:例如,从WKT和SRID创建和插入空间对象的语句是:
INSERT INTO geotable ( geom, name )
VALUES ( ST_GeomFromText('POINT(-126.4 45.32)', 312), 'A Place');
Well-Known Binary (WKB) provides a portable, full-precision representation of spatial data as binary data (arrays of bytes). Examples of the WKB representations of spatial objects are:
译:WKB提供了作为二进制数据(字节数组)的空间数据的可移植、全精度表示。空间对象的WKB表示法示例如下:
• WKT: POINT(1 1)
WKB: 0101000000000000000000F03F000000000000F03
• WKT: LINESTRING (2 2, 9 9)
WKB: 0102000000020000000000000000000040000000000000004000000000000022400000000000002240
Input and output of WKB is provided by the functions ST_AsBinary and ST_GeomFromWKB:
译:WKB的输入和输出由函数ST_AsBinary和ST_GeomFromWKB提供:
bytea WKB = ST_AsBinary(geometry);
geometry = ST_GeomFromWKB(bytea WKB, SRID);
For example, a statement to create and insert a spatial object from WKB is:
译:例如,从WKB创建和插入空间对象的语句是:
INSERT INTO geotable ( geom, name )
VALUES ( ST_GeomFromWKB('\x0101000000000000000000f03f000000000000f03f', 312), 'A Place');
4.2 Geometry Data Type
译:4.2 几何要素数据类型
PostGIS implements the OGC Simple Features model by defining a PostgreSQL data type called geometry. It represents all of the geometry subtypes by using an internal type code (see GeometryType and ST_GeometryType). This allows modelling spatial features as rows of tables defined with a column of type geometry.
译:PostGIS通过定义名为geometry的PostgreSQL数据类型实现了OGC标准简单要素模型。它使用内部类型代码表示所有几何图形子类型(请参见GeometryType和ST_GeometryType)。这将允许将空间要素作为表中带有要素列的记录行来模型化。
The geometry data type is opaque, which means that all access is done via invoking functions on geometry values. Functions allow creating geometry objects, accessing or updating all internal fields, and compute new geometry values. PostGIS supports all the functions specified in the OGC Simple feature access - Part 2: SQL option (SFS) specification, as well many others. See Chapter 8 for the full list of functions.
译:几何要素数据类型是不透明的,这意味着所有访问都是通过调用几何要素值上的函数来完成的。函数允许创建几何图形对象、访问或更新所有内部字段以及计算新的几何图形值。PostGIS支持OGC简单功能访问-第2部分:SQL选项(SFS)规范中指定的所有功能,以及许多其他功能。有关功能的完整列表,请参见第8章。
Note/笔记
PostGIS follows the SFA standard by prefixing spatial functions with "ST_". This was intended to stand for "Spatial and Temporal", but the temporal part of the standard was never developed. Instead it can be interpreted as "Spatial Type".
译:PostGIS遵循SFA标准,在空间函数前面加上“ST_”。这是为了代表“空间和时间”,但该标准的时间部分从未开发出来。相反,它可以被解释为“空间类型”。
The SFA standard specifies that spatial objects include a Spatial Reference System identifier (SRID). The SRID is required when creating spatial objects for insertion into the database (it may be defaulted to 0). See ST_SRID and Section 4.5.
译:SFA标准规定空间对象包括空间参考系统标识符(SRID)。创建要插入数据库的空间对象时需要SRID(它可能默认为0)。参见ST_SRID和第4.5节。
To make querying geometry efficient PostGIS defines various kinds of spatial indexes, and spatial operators to use them. See Section 4.9 and Section 5.2 for details.
译:为了提高查询几何图形的效率,PostGIS定义了各种空间索引,以及使用它们的空间运算符。详见第4.9节和第5.2节。
4.2.1 PostGIS EWKB and EWKT
译:4.2.1 PostGIS EWKB and EWKT
OGC SFA specifications initially supported only 2D geometries, and the geometry SRID is not included in the input/output representations. The OGC SFA specification 1.2.1 (which aligns with the ISO 19125 standard) adds support for 3D (ZYZ) and measured (XYM and XYZM) coordinates, but still does not include the SRID value.
译:OGC SFA规范最初仅支持2D几何图形,并且几何图形SRID不包含在输入/输出表示中。OGC SFA规范1.2.1(与ISO 19125标准一致)增加了对3D(ZYZ)和测量(XYM和XYZM)坐标的支持,但仍然不包括SRID值。
Because of these limitations PostGIS defined extended EWKB and EWKT formats. They provide 3D (XYZ and XYM) and 4D (XYZM) coordinate support and include SRID information. Including all geometry information allows PostGIS to use EWKB as the format of record (e.g. in DUMP files).
译:由于这些限制,PostGIS定义了扩展的EWKB和EWKT格式。它们提供3D(XYZ和XYM)和4D(XYZM)坐标支持,并包括SRID信息。通过包含所有几何信息,PostGIS可以使用EWKB作为记录格式(例如,在DUMP文件中)。
EWKB and EWKT are used for the "canonical forms" of PostGIS data objects. For input, the canonical form for binary data is EWKB, and for text data either EWKB or EWKT is accepted. This allows geometry values to be created by casting a text value in either HEXEWKB or EWKT to a geometry value using ::geometry. For output, the canonical form for binary is EWKB, and for text it is HEXEWKB (hex-encoded EWKB).
译:EWKB和EWKT用于PostGIS数据对象的“规范形式”。对于输入,二进制数据的规范形式是EWKB,而对于文本数据,则接受EWKB或EWKT。这允许通过使用:geometry将HEXEWKB或EWKT中的文本值强制转换为几何体值来创建几何体值。对于输出,二进制的规范形式是EWKB,而对于文本,它是HEXEWKB(十六进制编码的EWKB)。
For example this statement creates a geometry by casting from an EWKT text value, and outputs it using the canonical form of HEXEWKB:
译:例如,此语句通过从EWKT文本值进行强制转换来创建几何体,并使用HEXEWKB的规范形式输出它:
SELECT 'SRID=4;POINT(0 0)'::geometry;
geometry
----------------------------------------------------
01010000200400000000000000000000000000000000000000
PostGIS EWKT output has a few differences to OGC WKT:
译:PostGIS EWKT输出与OGC WKT有一些不同:
• For 3DZ geometries the Z qualifier is omitted/对于3DZ几何图形,省略Z限定符:
OGC: POINT Z (1 2 3)
EWKT: POINT (1 2 3)
• For 3DM geometries the M qualifier is included/对于3DM几何图形,包括M限定符:
OGC: POINT M (1 2 3)
EWKT: POINTM (1 2 3)
• For 4D geometries the ZM qualifier is omitted/对于4D几何形状,省略了ZM限定符:
OGC: POINT ZM (1 2 3 4)
EWKT: POINT (1 2 3 4)
EWKT avoids over-specifying dimensionality and the inconsistencies that can occur with the OGC/ISO format, such as:
译:EWKT避免了过度指定维度以及OGC/ISO格式可能出现的不一致,例如:
• POINT ZM (1 1)
• POINT ZM (1 1 1)
• POINT (1 1 1 1)
Caution/注意
PostGIS extended formats are currently a superset of the OGC ones, so that every valid OGC WKB/WKT is also valid EWKB/EWKT. However, this might vary in the future, if the OGC extends a format in a way that conflicts with the PosGIS definition. Thus you SHOULD NOT rely on this compatibility!
译:PostGIS扩展格式目前是OGC扩展格式的超集,因此每个有效的OGC WKB/WKT也是有效的EWKB/EWKT。然而,如果OGC以与PosGIS定义相冲突的方式扩展格式,这在未来可能会有所不同。因此,您不应该依赖这种兼容性!
Examples of the EWKT text representation of spatial objects are:
译:空间对象的EWKT文本表示的示例包括:
• POINT(0 0 0) -- XYZ
• SRID=32632;POINT(0 0) -- XY with SRID
• POINTM(0 0 0) -- XYM
• POINT(0 0 0 0) -- XYZM
• SRID=4326;MULTIPOINTM(0 0 0,1 2 1) -- XYM with SRID
• MULTILINESTRING((0 0 0,1 1 0,1 2 1),(2 3 1,3 2 1,5 4 1))
• POLYGON((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0))
• MULTIPOLYGON(((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0)),((-1 -1 0,-1 -2 0,-2 -2 0,-2 -1 0,-1 -1 0)))
• GEOMETRYCOLLECTIONM( POINTM(2 3 9), LINESTRINGM(2 3 4, 3 4 5) )
• MULTICURVE( (0 0, 5 5), CIRCULARSTRING(4 0, 4 4, 8 4) )
• POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0
0 0)), ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)), ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )
• TRIANGLE ((0 0, 0 10, 10 0, 0 0))
• TIN( ((0 0 0, 0 0 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 0 0 0)) )
Input and output using these formats is available using the following functions:
译:使用以下功能可以使用这些格式进行输入和输出:
bytea EWKB = ST_AsEWKB(geometry);
text EWKT = ST_AsEWKT(geometry);
geometry = ST_GeomFromEWKB(bytea EWKB);
geometry = ST_GeomFromEWKT(text EWKT);
For example, a statement to create and insert a PostGIS spatial object using EWKT is:
译:例如,使用EWKT创建和插入PostGIS空间对象的语句是:
INSERT INTO geotable ( geom, name )
VALUES ( ST_GeomFromEWKT('SRID=312;POINTM(-126.4 45.32 15)'), 'A Place' )
4.3 Geography Data Type
译:4.3 地理数据类型
The PostGIS geography data type provides native support for spatial features represented on "geographic" coordinates (sometimes called "geodetic" coordinates, or "lat/lon", or "lon/lat"). Geographic coordinates are spherical coordinates expressed in angular units (degrees).
译:PostGIS地理数据类型为以“地理”坐标(有时称为“大地测量”坐标,或“lat/lon”或“lon/lat”)表示的空间特征提供本地支持。地理坐标是以角度单位(度)表示的球面坐标。
The basis for the PostGIS geometry data type is a plane. The shortest path between two points on the plane is a straight line. That means functions on geometries (areas, distances, lengths, intersections, etc) are calculated using straight line vectors and cartesian mathematics. This makes them simpler to implement and faster to execute, but also makes them inaccurate for data on the spheroidal surface of the earth.
译:PostGIS几何图形数据类型的基础是平面。平面上两点之间的最短路径是一条直线。这意味着几何图形上的函数(面积、距离、长度、交点等)是使用直线向量和笛卡尔数学计算的。这使得它们实现起来更简单,执行起来更快,但也使得它们对地球椭球表面的数据不准确。
The PostGIS geography data type is based on a spherical model. The shortest path between two points on the sphere is a great circle arc. Functions on geographies (areas, distances, lengths, intersections, etc) are calculated using arcs on the sphere. By taking the spheroidal shape of the world into account, the functions provide more accurate results.
译:PostGIS地理数据类型基于球形模型。球体上两点之间的最短路径是一条大圆弧。使用球体上的圆弧计算地理函数(面积、距离、长度、交点等)。通过将世界的球体形状考虑在内,函数可以提供更准确的结果。
Because the underlying mathematics is more complicated, there are fewer functions defined for the geography type than for the geometry type. Over time, as new algorithms are added the capabilities of the geography type will expand. As a workaround one can convert back and forth between geometry and geography types.
译:因为基础数学更复杂,所以为地理类型定义的函数比为几何类型定义的更少。随着时间的推移,随着新算法的加入,地理类型的功能将得到扩展。作为一种变通方法,可以在几何图形和地理类型之间来回转换。
Like the geometry data type, geography data is associated with a spatial reference system via a spatial reference system identifier (SRID). Any geodetic (long/lat based) spatial reference system defined in the spatial_ref_sys table can be used. (Prior to PostGIS 2.2, the geography type supported only WGS 84 geodetic (SRID:4326)). You can add your own custom geodetic spatial reference system as described in Section 4.5.2.
译:与几何数据类型一样,地理数据通过空间参考系统标识符(SRID)与空间参考系统相关联。可以使用spatial_ref_sys表中定义的任何大地测量(基于长/纬度)空间参考系统。(在PostGIS 2.2之前,地理类型仅支持WGS 84大地测量(SRID:4326))。您可以添加自己的自定义大地测量空间参考系统,如第4.5.2节所述。
For all spatial reference systems the units returned by measurement functions (e.g. ST_Distance, ST_Length, ST_Perimeter, ST_Area) and for the distance argument of ST_DWithin are in meters.
译:对于所有空间参考系统,测量函数(例如ST_Distance、ST_Length、ST_Perimeter、ST_Area)和ST_DWithin的距离自变量返回的单位以米为单位。
4.3.1 Creating Geography Tables
译:4.3.1 创建地理表
You can create a table to store geography data using the CREATE TABLE SQL statement with a column of type geography. The following example creates a table with a geography column storing 2D LineStrings(笔者以为此处应为Points,大概率误笔) in the WGS84 geodetic coordinate system (SRID 4326):
译:您可以使用CREATETABLE SQL语句创建一个表来存储地理数据,该语句具有geography类型的列。以下示例创建了一个表,该表的地理列存储WGS84大地坐标系(SRID 4326)中的二维点:
CREATE TABLE global_points (
id SERIAL PRIMARY KEY,
name VARCHAR(64),
location geography(POINT,4326)
);
实践:借助sql与postgresql数据库实践如下:
脚本执行结果如下。
查看视图中geography_columns中记录显示如下:
The geography type supports two optional type modifiers:
译:地理类型支持两个可选的类型修饰符:
• the spatial type modifier restricts the kind of shapes and dimensions allowed in the
column. Values allowed for the spatial type are: POINT, LINESTRING, POLYGON, MULTIPOINT,
MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION. The geography type does not support
curves, TINS, or OLYHEDRALSURFACEs. The modifier supports coordinate dimensionality
restrictions by adding suffixes: Z, M and ZM. For example, a modifier of ’LINESTRINGM’ only
allowslinestrings with three dimensions, and treats the third dimension as a measure.
Similarly, ’POINTZM’ requires four dimensional (XYZM) data.
译:空间类型修饰符限制列中允许的形状和尺寸的种类。空间类型允许的值是:点,线性,多边形,多点,多线
性,多多边形,几何。地理类型不支持曲线、TINS或多边形曲面。修改器支持坐标通过添加后缀来限制维度:Z、
M和ZM。例如,'LINESTRING'的修饰符只允许linestring具有三个维度,并将第三个维度视为度量。类似地,
“POINTZM”需要四维(XYZM)数据。
• the SRID modifier restricts the spatial reference system SRID to a particular number. If
omitted, the SRID defaults to 4326(WGS84 geodetic), and all calculations are performed
using WGS84.
译:SRID修改器将空间参考系统SRID限制为特定的数字。如果省略,SRID默认为4326(WGS84大地测量),并且
所有计算都使用WGS84执行。
Examples of creating tables with geography columns:
译:创建具有地理列的表的示例:
• Create a table with 2D POINT geography with the default SRID 4326 (WGS84 long/lat):
译:使用默认SRID 4326(WGS84长/纬度)创建一个具有2D POINT地理位置的表格:
CREATE TABLE ptgeogwgs(gid serial PRIMARY KEY, geog geography(POINT) );
• Create a table with 2D POINT geography in NAD83 longlat:
译:在NAD83 longlat中创建一个具有2D POINT地理位置的表格:
CREATE TABLE ptgeognad83(gid serial PRIMARY KEY, geog geography(POINT,4269) );
• Create a table with 3D (XYZ) POINTs and an explicit SRID of 4326:
译:创建一个包含3D(XYZ)POINT和4326的明确SRID的表格:
CREATE TABLE ptzgeogwgs84(gid serial PRIMARY KEY, geog geography(POINTZ,4326) );
• Create a table with 2D LINESTRING geography with the default SRID 4326:
译:使用默认SRID 4326创建一个具有2D LINESTRING地理位置的表格:
CREATE TABLE lgeog(gid serial PRIMARY KEY, geog geography(LINESTRING) );
• Create a table with 2D POLYGON geography with the SRID 4267 (NAD 1927 long lat):
译:使用SRID 4267(NAD 1927长纬度)创建一个具有2D POLYGON地理位置的表格:
CREATE TABLE lgeognad27(gid serial PRIMARY KEY, geog geography(POLYGON,4267) );
实践:借助sql与postgresql数据库实践如下:
脚本执行结果如下:
查看表列表中,表记录如下:
查看视图中geography_columns中记录显示如下:
Geography fields are registered in the geography_columns system view. You can query the geography_columns view and see that the table is listed:
译:“地理”字段在“地理_列”系统视图中注册。您可以查询geographic_columns视图,并看到该表已列出:
SELECT * FROM geography_columns;
实践:借助sql与postgresql数据库实践如下:
Creating a spatial index works the same as for geometry columns. PostGIS will note that the column type is GEOGRAPHY and create an appropriate sphere-based index instead of the usual planar index used for GEOMETRY.
译:创建空间索引的工作原理与创建几何图形列的工作原理相同。PostGIS会注意到,列类型是GEOGRAPHY,并创建一个适当的基于球体的索引,而不是用于GEOMETRY的通常平面索引。
-- Index the test table with a spherical index/用球形索引为测试表编制索引
CREATE INDEX global_points_gix ON "global_points" USING GIST ( location );
实践:借助sql与postgresql数据库实践如下:
在表设计中查看索引属性如下:
4.3.2 Using Geography Tables
译:4.3.2 使用地理表
You can insert data into geography tables in the same way as geometry. Geometry data will autocast to the geography type if it has SRID 4326. The EWKT and EWKB formats can also be used to specify geography values.
译:可以使用与几何图形相同的方式将数据插入地理表中。如果几何数据具有SRID 4326,则几何数据将自动转换为地理类型。EWKT和EWKB格式也可用于指定地理值。
-- Add some data into the test table/添加一些数据到表
INSERT INTO global_points (name, location) VALUES ('Town', 'SRID=4326;POINT(-110 30)');
INSERT INTO global_points (name, location) VALUES ('Forest', 'SRID=4326;POINT(-109 29)');
INSERT INTO global_points (name, location) VALUES ('London', 'SRID=4326;POINT(0 49)');
实践:借助sql与postgresql数据库实践如下:
执行成功后,查询数据表结果如下:
Any geodetic (long/lat) spatial reference system listed in spatial_ref_sys table may be specified as a geography SRID. Non-geodetic coordinate systems raise an error if used.
译:spatial_ref_sys表中列出的任何大地测量(长/纬度)空间参考系统都可以指定为地理SRID。如果使用非大地坐标系,则会产生错误。
-- NAD 83 lon/lat
SELECT 'SRID=4269;POINT(-123 34)'::geography;
geography
----------------------------------------------------
0101000020AD1000000000000000C05EC00000000000004140
实践:借助sql与postgresql数据库实践如下:
-- NAD27 lon/lat
SELECT 'SRID=4267;POINT(-123 34)'::geography;
geography
----------------------------------------------------
0101000020AB1000000000000000C05EC00000000000004140
实践:借助sql与postgresql数据库实践如下:
-- NAD83 UTM zone meters - gives an error since it is a meter-based planar projection
-- NAD83 UTM区域米-由于是基于米的平面投影,因此会产生误差
SELECT 'SRID=26910;POINT(-123 34)'::geography;
ERROR: Only lon/lat coordinate systems are supported in geography.
实践:借助sql与postgresql数据库实践如下:
Query and measurement functions use units of meters. So distance parameters should be expressed in meters, and return values should be expected in meters (or square meters for areas).
译:查询和测量功能使用米为单位。因此,距离参数应以米表示,并返回值应以米为单位(或面积为平方米)。
-- A distance query using a 1000km tolerance
-- 使用1000公里公差的距离查询
SELECT name FROM global_points WHERE ST_DWithin(location, 'SRID=4326;POINT(-110 29)'::geography, 1000000);
实践:借助sql与postgresql数据库实践如下:
You can see the power of geography in action by calculating how close a plane flying a great circle route from Seattle to London (LINESTRING(-122.33 47.606, 0.0 51.5)) comes to Reykjavik (POINT(-21.96 64.15)) (map the route).
译:你可以通过计算一架从西雅图飞往伦敦的飞机(LINESTRING(-122.33 47.606,0.0 51.5))离雷克雅未克(POINT(-21.96 64.15))的距离来了解地理的力量(绘制路线)。
The geography type calculates the true shortest distance of 122.235 km over the sphere between Reykjavik and the great circle flight path between Seattle and London.
译:地理类型计算出雷克雅未克与西雅图和伦敦之间的大圆飞行路线之间的球体上122.235公里的真实最短距离。
-- Distance calculation using GEOGRAPHY
-- 用地理计算距离
SELECT ST_Distance('LINESTRING(-122.33 47.606, 0.0 51.5)'::geography, 'POINT(-21.96 64.15)'::geography);
st_distance
-----------------
122235.23815667
实践:借助sql与postgresql数据库实践如下:
The geometry type calculates a meaningless cartesian distance between Reykjavik and the straight line path from Seattle to London plotted on a flat map of the world. The nominal units of the result is "degrees", but the result doesn’t correspond to any true angular difference between the points, so even calling them "degrees" is inaccurate.
译:该几何类型计算了雷克雅未克和从西雅图到伦敦的直线路径之间的无意义笛卡尔距离,该直线路径绘制在平面世界地图上。结果的标称单位是“度”,但结果与点之间的任何真实角度差都不对应,因此即使将它们称为“度”也是不准确的。
-- Distance calculation using GEOMETRY
-- 使用几何计算距离
SELECT ST_Distance('LINESTRING(-122.33 47.606, 0.0 51.5)'::geometry, 'POINT(-21.96 64.15)'::geometry);
st_distance
--------------------
13.342271221453624
实践:借助sql与postgresql数据库实践如下:
4.3.3 When to use the Geography data type
译:4.3.3 何时使用地理数据类型
The geography data type allows you to store data in longitude/latitude coordinates, but at a cost: there are fewer functions defined on GEOGRAPHY than there are on GEOMETRY; those functions that are defined take more CPU time to execute.
译:地理数据类型允许您将数据存储在经度/纬度坐标中,但需要付出代价:在geography上定义的函数比在GEOMETRY上定义的更少;那些被定义的函数需要更多的CPU时间来执行。
The data type you choose should be determined by the expected working area of the application you are building. Will your data span the globe or a large continental area, or is it local to a state, county or municipality?
译:您选择的数据类型应该由您正在构建的应用程序的预期工作区域决定。您的数据会覆盖全球或大片大陆地区,还是州、县或市的本地数据?
• If your data is contained in a small area, you might find that choosing an appropriate projection and using GEOMETRY is the best solution, in terms of performance and
functionality available.
译:如果您的数据包含在一个小区域中,您可能会发现选择适当的投影并使用几何是最佳解决方案,在可用的性能和功能方面。
• If your data is global or covers a continental region, you may find that GEOGRAPHY allows you to build a system without having to worry about projection details. You store
your data in longitude/latitude, and use the functions that have been defined on GEOGRAPHY.
译:如果您的数据是全球性的或覆盖大陆地区,您可能会发现地理信息允许您在没有不得不担心投影细节。您将数据存储在经度/纬度中,并使用已定义的函数关于地理学。
• If you don’t understand projections, and you don’t want to learn about them, and you’re prepared to accept the limitations in functionality available in GEOGRAPHY, then it
might be easier for you to use GEOGRAPHY than GEOMETRY. Simply load your data up as longitude/latitude and go from there.
译:如果你不理解预测,也不想了解它们,并且你准备接受GEOGRAPHY中可用的功能,那么使用GEOGRAPHY可能比使用GEOMETRY更容易。简单加载你的数据作为经度/纬度,然后从那里开始。
Refer to Section 15.11 for compare between what is supported for Geography vs. Geometry. For a brief listing and description of Geography functions, refer to Section 15.4
译:参考第15.11节,比较地理与几何支持的内容。简单的列表和描述关于地理功能,请参阅第15.4节。
4.3.4 Geography Advanced FAQ
译:4.3.4 地理高级常见问题解答
1. Do you calculate on the sphere or the spheroid?
译:1.你是在球体上计算还是在椭球上计算?
By default, all distance and area calculations are done on the spheroid. You should find that the results of calculations in local areas match up will with local planar results in good local projections. Over larger areas, the spheroidal calculations will be more accurate than any calculation done on a projected plane. All the geography functions have the option of using a sphere calculation, by setting a final boolean parameter to ’FALSE’. This will somewhat speed up calculations, particularly for cases where the geometries are very simple.
译:默认情况下,所有距离和面积计算都在球体上完成。您应该会发现中的计算结果局部区域匹配将与良好的局部投影中的局部平面结果相匹配。在更大的面积上,球体计算将比在投影平面上进行的任何计算都更准确。所有地理功能都可以选择使用球体计算,通过将最终布尔参数设置为“FALSE”。这将在一定程度上加快计算,特别是在几何形状非常简单的情况下。
2. What about the date-line and the poles?
译:2.日期线和极点呢?
All the calculations have no conception of date-line or poles, the coordinates are spherical (longitude/latitude) so a shape that crosses the dateline is, from a calculation point of view, no different from any other shape.
译:所有的计算都没有日期线或极点的概念,坐标是球面(经度/纬度),因此从计算的角度来看,穿过日期线的形状与任何其他形状都没有什么不同。
3. What is the longest arc you can process?
译:你能处理的最长弧是什么?
We use great circle arcs as the "interpolation line" between two points. That means any two points are actually joined up two ways, depending on which direction you travel along the great circle. All our code assumes that the points are joined by the *shorter* of the two paths along the great circle. As a consequence, shapes that have arcs of more than 180 degrees will not be correctly modelled.
译:我们使用大圆弧作为两点之间的“插值线”。这意味着任何两个点实际上是以两种方式连接在一起的,这取决于你沿着大圆行进的方向。我们所有的代码都假设这些点由沿着大圆的两条路径中的较短路径连接。因此,圆弧超过180度的形状将无法正确建模。
4. Why is it so slow to calculate the area of Europe / Russia / insert big geographic region here ?
译:为什么计算欧洲/俄罗斯/在这里插入大地理区域的面积如此缓慢?
Because the polygon is so darned huge! Big areas are bad for two reasons: their bounds are huge, so the index tends to pull the feature no matter what query you run; the number of vertices is huge, and tests (distance, containment) have to traverse the vertex list at least once and sometimes N times (with N being the number of vertices in the other candidate feature). As with GEOMETRY, we recommend that when you have very large polygons, but are doing queries in small areas, you "denormalize" your geometric data into smaller chunks so that the index can effectively subquery parts of the object and so queries don’t have to pull out the whole object every time. Please consult ST_Subdivide function documentation. Just because you *can* store all of Europe in one polygon doesn’t mean you *should*.
译:因为这个多边形太大了!大区域之所以不好,有两个原因:它们的边界很大,所以无论运行什么查询,索引都倾向于拉取特性;顶点的数量是巨大的,并且测试(距离、容纳)必须遍历顶点列表至少一次,有时是N次(其中N是另一个候选特征中的顶点数量)。与GEOMETRY一样,我们建议当您有非常大的多边形,但在较小的区域进行查询时,将几何数据“反规范化”为较小的块,这样索引就可以有效地子查询对象的部分,因此查询不必每次都提取整个对象。请参阅ST_Subdive函数文档。仅仅因为你可以将整个欧洲存储在一个多边形中并不意味着你应该。
4.4 Geometry Validation
译:4.4 几何要素验证
PostGIS is compliant with the Open Geospatial Consortium’s (OGC) Simple Features specification. That standard defines the concepts of geometry being simple and valid. These definitions allow the Simple Features geometry model to represent spatial objects in a consistent and unambiguous way that supports efficient computation. (Note: the OGC SF and SQL/MM have the same definitions for simple and valid.)
译:PostGIS符合开放地理空间联盟 (OGC) 的简单特征规范。该标准定义了简单和有效的几何概念。这些定义允许简单特征几何模型以一致和明确的方式表示空间对象,从而支持高效计算。(注意:OGCSF和SQL/MM具有相同的定义,用于简单和有效。)
4.4.1 Simple Geometry
译:4.4.1 简单几何要素
A simple geometry is one that has no anomalous geometric points, such as self intersection or self tangency.
译:简单几何体是指没有不规则的几何点的几何要素,例如自交或自切。
A POINT is inherently simple as a 0-dimensional geometry object.
译:POINT本质上是简单的0维几何对象。
MULTIPOINTs are simple if no two coordinates (POINTs) are equal (have identical coordinate values).
译:如果不存在两个坐标相同的点,则MULTIPOINT也是简单几何要素。
A LINESTRING is simple if it does not pass through the same point twice, except for the endpoints. If the endpoints of a simple LineString are identical it is called closed and referred to as a Linear Ring.
译:如果不经过同一个点两次(短点除外),则LINESTRING是简单几何要素。如果一个简单的LINESTRING断电是相同的,则称它为闭合的,并称其为线性环(Linear Ring)。
(a) and (c) are simple LINESTRINGs. (b) and (d) are not simple. (c) is a closed Linear Ring.
译:(a) 和 (c) 是简单线, (b) 和 (d) 不是简单几何要素。 (c) 是闭合的线性环。
(a) |
(b) |
(c) |
(d) |
A MULTILINESTRING is simple only if all of its elements are simple and the only intersection between any two elements occurs at points that are on the boundaries of both elements.
译:MULTILINESTRING只有当它的所有元素都是简单的,并且任何两个元素之间的唯一交集发生在两个元素边界上的点上时,它才是简单的几何要素。
(e) and (f) are simple MULTILINESTRINGs. (g) is not simple.
译: (e) 和 (f) 是简单的多线。 (g) 不是简单几何要素。
(e) | (f) | (g) |
POLYGONs are formed from linear rings, so valid polygonal geometry is always simple.
译:多边形是由线性环形成的,所以有效的多边形几何都是简单几何要素。
To test if a geometry is simple use the ST_IsSimple function:
译:使用 ST_IsSimple 函数老测试一个几何要素是否为简单要素:
SELECT
ST_IsSimple('LINESTRING(0 0, 100 100)') AS straight,
ST_IsSimple('LINESTRING(0 0, 100 100, 100 0, 0 100)') AS crossing;
straight | crossing
---------+----------
t | f
实践:借助sql与postgresql数据库实践如下:
Generally, PostGIS functions do not require geometric arguments to be simple. Simplicity is primarily used as a basis for defining geometric validity. It is also a requirement for some kinds of spatial data models (for example, linear networks often disallow lines that cross). Multipoint and linear geometry can be made simple using ST_UnaryUnion.
译:通常PostGIS函数不需要简单的几何参数。简单性主要用作定义几何有效性的基础。这也是对某些类型的空间数据模型的要求(例:”线性网络通常不允许线交叉)。使用ST_UnaryUnion可以简化多点和线性几何体。
4.4.2 Valid Geometry
译:4.4.2 验证几何要素
Geometry validity primarily applies to 2-dimensional geometries (POLYGONs and MULTIPOLYGONs) . Validity is defined by rules that allow polygonal geometry to model planar areas unambiguously.
译:几何有效性主要适用于二维几何(多边形和多多边形)。有效性定义为允许多边形几何体对平面区域进行明确建模的规则。
A POLYGON is valid if:
译:如果满足一下情况则多边形是有效的:
1. the polygon boundary rings (the exterior shell ring and interior hole rings) are simple (do not cross or self-touch). Because of this a polygon cannnot have cut lines, spikes or loops. This implies that polygon holes must be represented as interior rings, rather than by the exterior ring self-touching (a so-called "inverted hole").
译:多边形边界环(外壳环和内孔环)很简单(不交叉或自接触)。正因为如此,多边形不能有剪切线、尖刺或环。这意味着多边形孔必须表示为内环,而不是外环自接触(所谓的“倒孔”)。
2. boundary rings do not cross
译:边界环不相交
3. boundary rings may touch at points but only as a tangent (i.e. not in a line)
译:边界环可以在点上接触,但只能作为切线(即不在直线上)
4. interior rings are contained in the exterior ring
译:内环包含在外环中
5. the polygon interior is simply connected (i.e. the rings must not touch in a way that splits the polygon into more than one part)
译:多边形内部是简单连接的(即环不能以将多边形分割成多个部分的方式接触)
(h) and (i) are valid POLYGONs. (j-m) are invalid. (j) can be represented as a valid MULTIPOLYGON.
译: (h) 和 (i) 是有效的多边形。 (j-m) 是无效的。 (j)可以表示为有效的多多边形。
(h) | (i) | (j) |
(k) | (l) | (m) |
A MULTIPOLYGON is valid if:
译:如果满足一下情况则多多边形是有效的:
1. its element POLYGONs are valid
译:其元素POLYGON有效
2. elements do not overlap (i.e. their interiors must not intersect)
译:元素不重叠(即其内部不得相交)
3. elements touch only at points (i.e. not along a line)
译:元素只在点上接触(即不沿着一条线)
(n) is a valid MULTIPOLYGON. (o) and (p) are invalid.
译:(n) 是有效的多多边形。 (o) 和 (p) 是无效的。
(n) | (o) | (p) |
These rules mean that valid polygonal geometry is also simple.
译:这些规则意味着有效的多边形几何体也是简单的。
For linear geometry the only validity rule is that LINESTRINGs must have at least two points and have non-zero length (or equivalently, have at least two distinct points.) Note that non-simple (self-intersecting) lines are valid.
译:对于线性几何体,唯一的有效性规则是LINESTRING必须至少有两个点并且具有非零长度(或者等效地,至少有两条不同的点)。请注意,非简单(自相交)线是有效的。
SELECT
ST_IsValid('LINESTRING(0 0, 1 1)') AS len_nonzero,
ST_IsValid('LINESTRING(0 0, 0 0, 0 0)') AS len_zero,
ST_IsValid('LINESTRING(10 10, 150 150, 180 50, 20 130)') AS self_int;
len_nonzero | len_zero | self_int
------------+----------+----------
t | f | t
实践:借助sql与postgresql数据库实践如下:
4.4.3 Managing Validity
译:4.4.3 管理有效性
PostGIS allows creating and storing both valid and invalid Geometry. This allows invalid geometry to be detected and flagged or fixed. There are also situations where the OGC validity rules are stricter than desired (examples of this are zero-length linestrings and polygons with inverted holes.)
译:PostGIS允许创建和存储有效和无效的几何图形。这允许检测、标记或修复无效的几何体。还有一些情况下,OGC有效性规则比期望的更严格(例如零长度的线串和带有倒孔的多边形)。
Many of the functions provided by PostGIS rely on the assumption that geometry arguments are valid. For example, it does not make sense to calculate the area of a polygon that has a hole defined outside of the polygon, or to construct a polygon from a non-simple boundary line. Assuming valid geometric inputs allows functions to operate more efficiently, since they do not need to check for topological correctness. (Notable exceptions are that zero-length lines and polygons with inversions are generally handled correctly.) Also, most PostGIS functions produce valid geometry output if the inputs are valid. This allows PostGIS functions to be chained together safely.
译:PostGIS提供的许多功能都依赖于几何参数有效的假设。例如,计算在多边形外部定义了孔的多边形的面积,或者从非简单边界线构造多边形,都是没有意义的。假设有效的几何输入允许函数更有效地操作,因为它们不需要检查拓扑正确性。(值得注意的例外情况是,通常可以正确处理长度为零的直线和具有反转的多边形。)此外,如果输入有效,大多数PostGIS函数都会生成有效的几何体输出。这使得PostGIS功能可以安全地链接在一起。
If you encounter unexpected error messages when calling PostGIS functions (such as "GEOS Intersection() threw an error!"), you should first confirm that the function arguments are valid. If they are not, then consider using one of the techniques below to ensure the data you are processing is valid.
译:如果在调用PostGIS函数时遇到意外错误消息(例如“GEOS Intersection()抛出错误!”),则应首先确认函数参数是否有效。如果不是,请考虑使用以下技术之一,以确保您正在处理的数据是有效的。
Note/笔记
If a function reports an error with valid inputs, then you may have found an error in either PostGIS or one of the libraries it uses, and you should report this to the PostGIS project. The same is true if a PostGIS function returns an invalid geometry for valid input.
译:如果一个函数报告了一个带有有效输入的错误,那么您可能在PostGIS或它使用的某个库中发现了错误,您应该将其报告给PostGIS项目。如果PostGIS函数为有效输入返回无效的几何图形,情况也是如此。
To test if a geometry is valid use the ST_IsValid function:
译:使用ST_IsValid函数来测试几何要素是否有效:
SELECT ST_IsValid('POLYGON ((20 180, 180 180, 180 20, 20 20, 20 180))');
-----------------
t
实践:借助sql与postgresql数据库实践如下:
Information about the nature and location of an geometry invalidity are provided by the ST_IsValidDetail function:
译:几何要素有效性、原因和坐标信息可以通过ST_IsValidDetail函数查看:
SELECT valid, reason, ST_AsText(location) AS location
FROM ST_IsValidDetail('POLYGON ((20 20, 120 190, 50 190, 170 50, 20 20))') AS t;
valid | reason | location
------+-------------------+---------------------------------------------
f | Self-intersection | POINT(91.51162790697674 141.56976744186045)
实践:借助sql与postgresql数据库实践如下:
In some situations it is desirable to correct invalid geometry automatically. Use the ST_MakeValid function to do this. (ST_MakeValidis a case of a spatial function that does allow invalid input!)
译:在某些情况下,需要自动更正无效的几何图形。使用ST_MakeValid功能执行此操作。(ST_MakeValid是允许无效输入的空间函数的情况!)
By default, PostGIS does not check for validity when loading geometry, because validity testing can take a lot of CPU time for complex geometries. If you do not trust your data sources, you can enforce a validity check on your tables by adding a check constraint:
译:默认情况下,PostGIS在加载几何图形时不会检查有效性,因为有效性测试可能会占用复杂几何图形的大量CPU时间。如果您不信任数据源,可以通过添加检查约束来对表强制执行有效性检查:
ALTER TABLE mytable
ADD CONSTRAINT geometry_valid_check
CHECK (ST_IsValid(geom));