MySQL spatial query
Article Directory
1. Spatial data support
MySQL has spatial data types that correspond to OpenGIS classes. Some spatial data types contain a single geometry value:
GEOMETRY
POINT
LINESTRING
POLYGON
GEOMETRY
Geometry values of any type can be stored. Other distinct types ( POINT
, , LINESTRING
and POLYGON
) restrict their values to specific geometry types.
Other spatial data types contain collections of values:
MULTIPOINT
MULTILINESTRING
MULTIPOLYGON
GEOMETRYCOLLECTION
GEOMETRYCOLLECTION
A collection of objects of any type can be stored. The other collection types ( MULTIPOINT
, MULTILINESTRING
, and MULTIPOLYGON
) restrict collection members to those with a specific geometry type.
Example: To create a table named geom
with a column named that g
can store values of any geometry type, use the following statement:
CREATE TABLE geom (g GEOMETRY);
2. Spatial function support
The following table lists each spatial function and provides a short description of each function.
Table 1 Spatial functions
Name | describe | introduce |
---|---|---|
GeomCollection() |
Construct geometry collection from geometry | |
GeometryCollection() |
Construct geometry collection from geometry | |
LineString() |
Constructs a LineString from a point value | |
MBRContains() |
Whether the MBR of one geometry contains the MBR of another geometry | |
MBRCoveredBy() |
Whether one MBR is overwritten by another MBR | |
MBRCovers() |
Whether one MBR overwrites another | |
MBRDisjoint() |
Whether the MBRs of two geometries are disjoint | |
MBREquals() |
Are the MBRs of the two geometries equal | |
MBRIntersects() |
Whether the MBRs of the two geometries intersect | |
MBROverlaps() |
Whether the MBRs of the two geometries overlap | |
MBRTouches() |
Whether the MBRs of the two geometries touch | |
MBRWithin() |
Whether the MBR of one geometry is within the MBR of another | |
MultiLineString() |
Constructs a MultiLineString from LineString values | |
MultiPoint() |
Constructs a multipoint from point values | |
MultiPolygon() |
Constructs a MultiPolygon from Polygon values | |
Point() |
Construct points from coordinates | |
Polygon() |
Constructs a polygon from a LineString parameter | |
ST_Area() |
Returns a polygon or polygon area | |
ST_AsBinary() ,ST_AsWKB() |
Convert from internal geometry format to WKB | |
ST_AsGeoJSON() |
Generate a GeoJSON object from a geometry | |
ST_AsText() ,ST_AsWKT() |
Convert from internal geometry format to WKT | |
ST_Buffer() |
Returns the geometry for points within a given distance from the geometry | |
ST_Buffer_Strategy() |
Generate strategy options for ST_Buffer() | |
ST_Centroid() |
return the centroid as a point | |
ST_Collect() |
Aggregate spatial values into collections | 8.0.24 |
ST_Contains() |
Whether one geometry contains another | |
ST_ConvexHull() |
Returns the convex hull of the geometry | |
ST_Crosses() |
Whether a geometry intersects another geometry | |
ST_Difference() |
Returns point set difference of two geometries | |
ST_Dimension() |
Geometry | |
ST_Disjoint() |
Whether one geometry is disjoint from another | |
ST_Distance() |
the distance of one geometry from another | |
ST_Distance_Sphere() |
The smallest distance between two geometries on Earth | |
ST_EndPoint() |
end of linestring | |
ST_Envelope() |
Returns the MBR of the geometry | |
ST_Equals() |
Is one geometry equal to another | |
ST_ExteriorRing() |
Returns the outer ring of the polygon | |
ST_FrechetDistance() |
Discrete Fréchet distance of one geometry from another | 8.0.23 |
ST_GeoHash() |
Generate a geohash value | |
ST_GeomCollFromText() , ST_GeometryCollectionFromText() ,ST_GeomCollFromTxt() |
Return geometry collection from WKT | |
ST_GeomCollFromWKB() ,ST_GeometryCollectionFromWKB() |
Return geometry collection from WKB | |
ST_GeometryN() |
Returns the Nth geometry from the collection of geometries | |
ST_GeometryType() |
Returns the name of the geometry type | |
ST_GeomFromGeoJSON() |
Generate geometries from GeoJSON objects | |
ST_GeomFromText() ,ST_GeometryFromText() |
return geometry from WKT | |
ST_GeomFromWKB() ,ST_GeometryFromWKB() |
Return geometry from WKB | |
ST_HausdorffDistance() |
Discrete Hausdorff distance of one geometry from another | 8.0.23 |
ST_InteriorRingN() |
Returns the Nth inner ring of the polygon | |
ST_Intersection() |
Returns the point set intersection of two geometries | |
ST_Intersects() |
Whether a geometry intersects another geometry | |
ST_IsClosed() |
Is the geometry closed and simple | |
ST_IsEmpty() |
whether the geometry is empty | |
ST_IsSimple() |
Is the geometry simple | |
ST_IsValid() |
Is the geometry valid | |
ST_LatFromGeoHash() |
Return latitude from geohash value | |
ST_Latitude() |
point returns latitude | 8.0.12 |
ST_Length() |
Returns the length of the LineString | |
ST_LineFromText() ,ST_LineStringFromText() |
Build LineString from WKT | |
ST_LineFromWKB() ,ST_LineStringFromWKB() |
Construct LineString from WKB | |
ST_LineInterpolatePoint() |
points along the LineString at a given percentage | 8.0.24 |
ST_LineInterpolatePoints() |
points along the LineString at a given percentage | 8.0.24 |
ST_LongFromGeoHash() |
Return longitude from geohash value | |
ST_Longitude() |
Returns the longitude of the point | 8.0.12 |
ST_MakeEnvelope() |
rectangle around two points | |
ST_MLineFromText() ,ST_MultiLineStringFromText() |
Build MultiLineString from WKT | |
ST_MLineFromWKB() ,ST_MultiLineStringFromWKB() |
Build MultiLineString from WKB | |
ST_MPointFromText() ,ST_MultiPointFromText() |
Construct multipoint from WKT | |
ST_MPointFromWKB() ,ST_MultiPointFromWKB() |
Construct multipoint from WKB | |
ST_MPolyFromText() ,ST_MultiPolygonFromText() |
Build polygons from WKT | |
ST_MPolyFromWKB() ,ST_MultiPolygonFromWKB() |
Build polygons from WKB | |
ST_NumGeometries() |
Returns the number of geometries in the geometry collection | |
ST_NumInteriorRing() ,ST_NumInteriorRings() |
Returns the number of interior rings in the polygon | |
ST_NumPoints() |
Returns the number of points in the LineString | |
ST_Overlaps() |
Whether one geometry overlaps another | |
ST_PointAtDistance() |
a point along the LineString at a given distance | 8.0.24 |
ST_PointFromGeoHash() |
Convert geohash values to POINT values | |
ST_PointFromText() |
Construct points from WKT | |
ST_PointFromWKB() |
Construct points from WKB | |
ST_PointN() |
Returns the Nth point from a LineString | |
ST_PolyFromText() ,ST_PolygonFromText() |
Construct polygon from WKT | |
ST_PolyFromWKB() ,ST_PolygonFromWKB() |
Build polygons from WKB | |
ST_Simplify() |
return simplified geometry | |
ST_SRID() |
Returns the spatial reference system ID of the geometry | |
ST_StartPoint() |
start of linestring | |
ST_SwapXY() |
Swap return parameters for X/Y coordinates | |
ST_SymDifference() |
Returns the point set symmetric difference of two geometries | |
ST_Touches() |
Whether one geometry touches another | |
ST_Transform() |
transform geometric coordinates | 8.0.13 |
ST_Union() |
Returns the union of point sets of two geometries | |
ST_Validate() |
return validation geometry | |
ST_Within() |
Whether a geometry is inside another geometry | |
ST_X() |
Returns the X coordinate of Point | |
ST_Y() |
返回 Point 的 Y 坐标 |
3. 操作示例
效果如下:
3.1 创建表结构
DROP TABLE IF EXISTS target_polygon;
CREATE TABLE `target_polygon` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`out_line` geometry NOT NULL SRID 4326, -- 指定4326(WGS-84)坐标系
PRIMARY KEY (`id`),
SPATIAL INDEX (`out_line`) -- 空间索引,提高空间查询速度
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.2 插入数据
# 插入源格式为GeoJSON的数据
INSERT INTO target_polygon VALUES (1,ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[117.205262,31.870465],[117.254387,31.872216],[117.242555,31.847807],[117.216171,31.845654],[117.205262,31.870465]]]}')); -- 注意首尾点闭合,否则插入报错
INSERT INTO target_polygon VALUES (2,ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[117.307531,31.841532],[117.354107,31.833384],[117.339544,31.799192],[117.306117,31.815102],[117.307531,31.841532]]]}'));
INSERT INTO target_polygon VALUES (3,ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[117.353999,31.737498],[117.40432,31.745964],[117.395446,31.723819],[117.357326,31.721851],[117.353999,31.737498]]]}'));
3.3 查询空间相交数据
# 定义查询多边形
SET @queryPolygon = CONCAT('{"type":"Polygon","coordinates":[[[117.293996,31.884109],[117.493224,31.883049],[117.441397,31.694935],[117.295797,31.702517],[117.293996,31.884109]]]}');
# 使用变量,查询相交部分
select *,ST_AsGeoJSON(out_line) geojson from target_polygon where ST_INTERSECTS( out_line, ST_GeomFromGeoJSON(@queryPolygon) ); -- 将查询结果转为GeoJSON格式