Using a geographic coordinate system may introduce complications for
the developer later on. For example, PostGIS does not have the
capability to perform distance calculations between non-point
geometries using geographic coordinate systems, e.g., constructing a
query to find all points within 5 miles of a county boundary stored as
WGS84. [6]
What does this exactly mean if I want to use PostGIS and to be able to
do the searches described above across the USA? The docs suggest
using a projected coordinate system to cover only a specific region.
I need to cover the whole country so this I suppose is not an option.
Basically in the end I want to be able to find neighbouring ZIP codes
given a starting ZIP code. I don't really care how this is done on a
technical level.
Also where would I find a database that contains the geographic
boundaries of ZIP codes in the USA that I can import into a GeoDjango
model?
> What does this exactly mean if I want to use PostGIS and to be able to
> do the searches described above across the USA? The docs suggest
> using a projected coordinate system to cover only a specific region.
> I need to cover the whole country so this I suppose is not an option.
What this means is that in versions of PostGIS 1.4 and below, only
Cartesian distance is calculated -- the curvature of the earth is not
taken into account. GeoDjango (really PostGIS) only takes into account
the curvature of the earth when calculating distances from point
geometries to other point geometries. PostGIS 1.5 has something called
a "geography" type which removes this limitation, but this support won't
be included until Django 1.2.
> Basically in the end I want to be able to find neighbouring ZIP codes
> given a starting ZIP code. I don't really care how this is done on a
> technical level.
No distance calculations are required to do this, simply create a buffer
of the geometry with a small 'skirt', and then do a query of all ZIP
codes that intersect the buffer:
>>> z = Zipcode.objects.get(code='77002')
>>> buf = z.mpoly.buffer(0.0001)
>>> qs = Zipcode.objects.filter(mpoly__intersects=buf)
> Also where would I find a database that contains the geographic
> boundaries of ZIP codes in the USA that I can import into a GeoDjango
> model?
This is a shapefile that includes all the ZIP code tabulation areas for
the U.S.:
ftp://ftp2.census.gov/geo/tiger/TIGER2008/tl_2008_us_zcta5.zip
Here's an example models file:
class Zipcode(models.Model):
code = models.CharField(max_length=5, db_index=True)
mpoly = models.MultiPolygonField()
objects = models.GeoManager()
def __unicode__(self):
return self.code
And you could load it with the following script:
from django.contrib.gis.utils import LayerMapping
from census.models import Zipcode
lm = LayerMapping(Zipcode, 'tl_2008_us_zcta5.shp',
{ 'code' : 'ZCTA5CE',
'mpoly' : 'MULTIPOLYGON',
})
lm.save()
Best Regards,
-Justin
I wrote something incorrectly in my first post. When I asked how to
find neighbouring ZIP codes, I really meant how do I find ZIP codes
that are 5 miles from another ZIP code.
Since the ZIP codes are represented as MultiPolygonFields, and PostGIS
1.4 cannot calculate the distance between non-point geometries without
a projected coordinate sytem, does it make sense to treat each ZIP
code as a point to do the distance calculation?
High accuracy is not important to me. I am simply interested in using
GeoDjango in a search function to return locally relevant results
relative to a starting location.