10. 几何练习¶
以下是对我们迄今为止所见过的所有函数的提醒。它们应该对练习很有帮助!
sum(expression) 聚合以返回一组记录的总和
count(expression) 聚合以返回一组记录的大小
ST_GeometryType(geometry) 返回几何体的类型
ST_NDims(geometry) 返回几何体的维数
ST_SRID(geometry) 返回几何体的空间参考标识符编号
ST_X(point) 返回 X 坐标
ST_Y(point) 返回 Y 坐标
ST_Length(linestring) 返回线字符串的长度
ST_StartPoint(geometry) 返回第一个坐标作为点
ST_EndPoint(geometry) 返回最后一个坐标作为点
ST_NPoints(geometry) 返回线字符串中的坐标数量
ST_Area(geometry) 返回多边形的面积
ST_NRings(geometry) 返回环的数量(通常为 1,如果有孔则更多)
ST_ExteriorRing(polygon) 返回外环作为线字符串
ST_InteriorRingN(polygon, integer) 返回指定的内环作为线字符串
ST_Perimeter(geometry) 返回所有环的长度
ST_NumGeometries(multi/geomcollection) 返回集合中的部分数量
ST_GeometryN(geometry, integer) 返回集合中的指定部分
ST_GeomFromText(text) 返回
geometry
ST_AsText(geometry) 返回 WKT
text
ST_AsEWKT(geometry) 返回 EWKT
text
ST_GeomFromWKB(bytea) 返回
geometry
ST_AsBinary(geometry) 返回 WKB
bytea
ST_AsEWKB(geometry) 返回 EWKB
bytea
ST_GeomFromGML(text) 返回
geometry
ST_AsGML(geometry) 返回 GML
text
ST_GeomFromKML(text) 返回
geometry
ST_AsKML(geometry) 返回 KML
text
ST_AsGeoJSON(geometry) 返回 JSON
text
ST_AsSVG(geometry) 返回 SVG
text
还要记住我们可用的表格
nyc_census_blocks
blkid, popn_total, boroname, geom
nyc_streets
name, type, geom
nyc_subway_stations
name, geom
nyc_neighborhoods
name, boroname, geom
10.1. 练习¶
“西村”街区的面积是多少?
SELECT ST_Area(geom) FROM nyc_neighborhoods WHERE name = 'West Village';
1044614.5296486
注意
面积以平方米为单位。要获得公顷面积,请除以 10000。要获得英亩面积,请除以 4047。
“Pelham St”的几何类型是什么?长度是多少?
SELECT ST_GeometryType(geom), ST_Length(geom) FROM nyc_streets WHERE name = 'Pelham St';
ST_MultiLineString 50.323
“Broad St”地铁站的 GeoJSON 表示是什么?
SELECT ST_AsGeoJSON(geom) FROM nyc_subway_stations WHERE name = 'Broad St';
{"type":"Point", "crs":{"type":"name","properties":{"name":"EPSG:26918"}}, "coordinates":[583571.905921312,4506714.341192182]}
**纽约市街道的总长度(以公里为单位)是多少?**(提示:空间数据的测量单位是米,1 公里等于 1000 米。)
SELECT Sum(ST_Length(geom)) / 1000 FROM nyc_streets;
10418.9047172
**曼哈顿的面积是多少英亩?**(提示:
nyc_census_blocks
和nyc_neighborhoods
都有一个boroname
。)SELECT Sum(ST_Area(geom)) / 4047 FROM nyc_neighborhoods WHERE boroname = 'Manhattan';
13965.3201224118
或者…
SELECT Sum(ST_Area(geom)) / 4047 FROM nyc_census_blocks WHERE boroname = 'Manhattan';
14601.3987215548
最西边的地铁站是哪个?
SELECT ST_X(geom), name FROM nyc_subway_stations ORDER BY ST_X(geom) LIMIT 1;
Tottenville
“Columbus Cir”(又名哥伦布圆环)有多长?
SELECT ST_Length(geom) FROM nyc_streets WHERE name = 'Columbus Cir';
308.34199
纽约市街道的长度,按类型汇总?
SELECT type, Sum(ST_Length(geom)) AS length FROM nyc_streets GROUP BY type ORDER BY length DESC;
type | length --------------------------------------------------+------------------ residential | 8629870.33786606 motorway | 403622.478126363 tertiary | 360394.879051303 motorway_link | 294261.419479668 secondary | 276264.303897926 unclassified | 166936.371604458 primary | 135034.233017947 footway | 71798.4878378096 service | 28337.635038596 trunk | 20353.5819826076 cycleway | 8863.75144825929 pedestrian | 4867.05032825026 construction | 4803.08162103562 residential; motorway_link | 3661.57506293745 trunk_link | 3202.18981240201 primary_link | 2492.57457083536 living_street | 1894.63905457332 primary; residential; motorway_link; residential | 1367.76576941335 undefined | 380.53861910346 steps | 282.745221342127 motorway_link; residential | 215.07778911517
注意
ORDER BY length DESC
子句按长度降序对结果进行排序。结果是最普遍的类型在列表中排在最前面。