PostGIS教程八:关于几何图形的练习

一、函数列表

    以下是我们迄今为止看到的所有函数的提示,它们应该对练习有用!

  • 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

    还有请记住我们现在数据库中已经有的表:

  • nyc_census_blocks
    • blkid, popn_total, boroname, geom
  • nyc_streets
    • name, type, geom
  • nyc_subway_stations
    • name, geom
  • nyc_neighborhoods
    • name, boroname, geom

二、练习

    ①'West Village'社区(neighborhood)的面积是多少?

 
  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 - 行政区名)

 
  1. SELECT Sum(ST_Area(geom)) / 4047

  2. FROM nyc_neighborhoods

  3. WHERE boroname = 'Manhattan';

    或者:

 
  1. SELECT Sum(ST_Area(geom)) / 4047

  2. FROM nyc_census_blocks

  3. WHERE boroname = 'Manhattan';

    ③纽约市(New York City)有多少个人口普查块(census blocks)多边形里有孔洞?

 
  1. SELECT Count(*)

  2. FROM nyc_census_blocks

  3. WHERE ST_NumInteriorRings(ST_GeometryN(geom,1)) > 0;

    注意:ST_NRings()函数可能让人感觉可以胜任,但是它也计算多-多边形的外环和内环。为了运行ST_NumInteriorRings(),我们需要将MultiPolygon几何图形转换为简单的多边形,因此,我们使用ST_GeometryN()从每个集合中提取第一个多边形

    ④纽约市(New York)的街道总长度(公里)是多少?(提示:空间数据的测量单位是,每公里有1000米)

 
  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表示是怎样的?

 
  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子句按长度以降序形式排序。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值