postGIS關於幾何圖形的練習

轉發:https://blog.csdn.net/qq_35732147/article/details/85338695

1、函數列表

    如下是咱們迄今爲止看到的全部函數的提示,它們應該對練習有用!express

  • sum(expression) aggregate to return a sum for a set of records
  • count(expression) aggregate to return the size of a set of records
  • ST_GeometryType(geometry) returns the type of the geometry
  • ST_NDims(geometry) returns the number of dimensions of the geometry
  • ST_SRID(geometry) returns the spatial reference identifier number of the geometry
  • ST_X(point) returns the X ordinate
  • ST_Y(point) returns the Y ordinate
  • ST_Length(linestring) returns the length of the linestring
  • ST_StartPoint(geometry) returns the first coordinate as a point
  • ST_EndPoint(geometry) returns the last coordinate as a point
  • ST_NPoints(geometry) returns the number of coordinates in the linestring
  • ST_Area(geometry) returns the area of the polygons
  • ST_NRings(geometry) returns the number of rings (usually 1, more if there are holes)
  • ST_ExteriorRing(polygon) returns the outer ring as a linestring
  • ST_InteriorRingN(polygon, integer) returns a specified interior ring as a linestring
  • ST_Perimeter(geometry) returns the length of all the rings
  • ST_NumGeometries(multi/geomcollection) returns the number of parts in the collection
  • ST_GeometryN(geometry, integer) returns the specified part of the collection
  • ST_GeomFromText(text) returns geometry
  • ST_AsText(geometry) returns WKT text
  • ST_AsEWKT(geometry) returns EWKT text
  • ST_GeomFromWKB(bytea) returns geometry
  • ST_AsBinary(geometry) returns WKB bytea
  • ST_AsEWKB(geometry) returns EWKB bytea
  • ST_GeomFromGML(text) returns geometry
  • ST_AsGML(geometry) returns GML text
  • ST_GeomFromKML(text) returns geometry
  • ST_AsKML(geometry) returns KML text
  • ST_AsGeoJSON(geometry) returns JSON text
  • ST_AsSVG(geometry) returns SVG text

2、練習

    ①'West Village'社區(neighborhood)的面積是多少?ide

  1.   SELECT ST_Area(geom)
  2.   FROM nyc_neighborhoods
  3.   WHERE name = 'West Village';

    注意:面積以平方米爲單位。要獲得一個以公頃爲單位的面積,須要再對其除以10000;要獲得一個以英畝爲單位的面積,須要對其除以4047。函數

    ②曼哈頓(Manhattan)行政區的面積是多少英畝?(提示:nyc_census_blocks和nyc_neighborhoods中都有boroname - rorough name - 行政區名)spa

  1.   SELECT Sum(ST_Area(geom)) / 4047
  2.   FROM nyc_neighborhoods
  3.   WHERE boroname = 'Manhattan';

    或者:.net

  1.   SELECT Sum(ST_Area(geom)) / 4047
  2.   FROM nyc_census_blocks
  3.   WHERE boroname = 'Manhattan';

    ③紐約市(New York City)有多少我的口普查塊(census blocks)多邊形裏有孔洞?3d

  1.   SELECT Count(*)
  2.   FROM nyc_census_blocks
  3.   WHERE ST_NumInteriorRings(ST_GeometryN(geom,1)) > 0;

    注意:ST_NRings()函數可能讓人感受能夠勝任,可是它也計算多-多邊形的外環和內環。爲了運行ST_NumInteriorRings(),咱們須要將MultiPolygon幾何圖形轉換爲簡單的多邊形,所以,咱們使用ST_GeometryN()從每一個集合中提取第一個多邊形。code

    ④紐約市(New York)的街道總長度(千米)是多少?(提示:空間數據的測量單位是米,每千米有1000米)blog

  1.   SELECT Sum(ST_Length(geom)) / 1000
  2.   FROM nyc_streets;

    ⑤'Columbus Cir'(哥倫布圓環——紐約曼哈頓區的一個地標)有多長?排序

  1.   SELECT ST_Length(geom)
  2.   FROM nyc_streets
  3.   WHERE name = 'Columbus Cir';

    ⑥West Village社區邊界的JSON表示是怎樣的?ci

  1.   SELECT ST_AsGeoJSON(geom)
  2.   FROM nyc_neighborhoods
  3.   WHERE name = 'West Village';

    返回的JSON裏的幾何類型是"MultiPolygon(多多邊形)",有趣!

    ⑦West Village社區多多邊形(MultiPolygon)中有多少個多邊形?

  1.   SELECT ST_NumGeometries(geom)
  2.   FROM nyc_neighborhoods
  3.   WHERE name = 'West Village';

    注意:在空間表中找到單元素多多邊形並很多見。使用多多邊形容許只有一種幾何圖形類型的表同時存儲單(single-)幾何圖形和多(multi-)幾何圖形,而沒必要使用GeometryCollection類型。

    ⑧按類型(type)列出紐約市街道長度是多少?

  1.   SELECT type, Sum(ST_Length(geom)) AS length
  2.   FROM nyc_streets
  3.   GROUP BY type
  4.   ORDER BY length DESC;

    注意:ORDER BY length DESC子句按長度以降序形式排序。

相關文章
相關標籤/搜索