Database Reference
In-Depth Information
Measuring distances
In this recipe, we will check out the PostGIS functions needed for distance meas-
urements ( ST_Distance and its variants) and find out how considering the earth's
curvature makes a big difference when measuring distances between distant points.
Getting ready
YoushouldimporttheshapefilerepresentingthecitiesfromtheUSAthatwegener-
ated in a previous recipe (the PostGIS table named chp03.cities ). In case you
haven't already done so, download that shapefile from the nationalatlas.gov web-
site at http://dds.cr.usgs.gov/pub/data/nationalatlas/citiesx020_nt00007.tar.gz (this
archive is also included in the code bundle available with this topic) and import it to
PostGIS:
$ ogr2ogr -f PostgreSQL -s_srs EPSG:4269 -t_srs
EPSG:4326 -lco GEOMETRY_NAME=the_geom -nln
chp03.cities PG:"dbname='postgis_cookbook'
user='me' password='mypassword'" citiesx020.shp
How to do it...
The steps you need to perform to complete this recipe are as follows:
1. First,usethe ST_Distance functiontocalculatethedistancesbetweencities
in the USA that have more than 1,000,000 inhabitants using the Spheric-
alMercatorplanarprojectioncoordinatesystem(EPSG:900913,EPSG:3857,
or EPSG:3785; all of these SRID representations are equivalent). Use the
ST_Transform functionasfollowstoconvertthepointcoordinatesfromlon
latdegrees(asthecoordinatesareoriginallyinEPSG:4326)toaplanarmetric
system, if you want the results in meters:
postgis_cookbook=# SELECT c1.name, c2.name,
ST_Distance(ST_Transform(c1.the_geom,
900913), ST_Transform(c2.the_geom,
900913))/1000 AS distance_900913
Search WWH ::




Custom Search