views:

382

answers:

2

I'm trying to determine the neighborhood for a location, based on Zillow's freely published Shapefile data.

I don't really know anything about the Shapefile format, and am having some trouble finding tutorials online -- but I basically want to take latitude/longitude pairs, and run it against the Shapefile data to determine the corresponding neighborhood(s).

Can anyone point me in the right direction? Not even sure where to start.

This is where I've grabbed the Shapefile files: http://www.zillow.com/howto/api/neighborhood-boundaries.htm

A: 

In case you have not already received an answer on this topic, the method in which you are suggesting is not the most efficient mechanism. I'll try to break out the idea of how you can do this in a more effective way.

Start by importing your shapefile into a GeoSpatial database/datastore.

The lat/lon pattern of comparison can still be used, but it is only going to cause more work for you. Geo spatial databases/datastores provide functions to compare geometries (point, line, polygon). Instead of using a latitude and longitude, all geometry types are stored within a single geometry column. The functions then allow you to compare distance, size, overlay, intersection, and other comparisons.

How does the shapefile translate?

A shapefile is a collection of tabular data, think neighborhood name and additional fields, and a way to map those records to a spatially represented location. It is a geospatial database in a grouping of files. The comparison that comes to mind is someone using access as a database versus a traditional server database. There are toolsets/libraries available that will convert a shapefile to a geospatial database. You can find more information by searching for GDAL.

Hope this has been helpful.

Chad Pry
+1  A: 

Here's an example using PostGIS, the spatial extensions to postgresql. Similar extensions exist for mysql, oracle, mssql, sqlite and no doubt other databases. PostGIS must be installed for this to work.

First, you must convert the shapefile to an sql file:

fmark@fmark-laptop:~$ shp2pgsql -c Desktop/zillow/ZillowNeighborhoods-AK.shp zillowak > Desktop/zillow/ZillowNeighborhoods-AK.sql
Shapefile type: Polygon
Postgis type: MULTIPOLYGON[2]

Then run the sql on the database (database "gis" in this case) to convert the sql file to a spatially enabled table:

fmark@fmark-laptop:~$ psql -d gis -f Desktop/zillow/ZillowNeighborhoods-AK.sql
SET
BEGIN
...
COMMIT

At this point you probably want to create a spatial index, as you are going to be doing a spatial query:

fmark@fmark-laptop:~$ psql -d gis
psql (8.4.2)
Type "help" for help.

gis=# CREATE INDEX idx_neighborhoods ON zillowak USING gist(the_geom);
CREATE INDEX
gis-# \q

Now, if you have a lat/long (in this example -149.309W, 60.985S), you can find which neighborhood it is in: fmark@fmark-laptop:~$ psql -d gis psql (8.4.2) Type "help" for help.

gis=# select gid, state, county, city, name, regionid from zillowak WHERE ST_CONTAINS(the_geom, GeomFromText('POINT(-149.309 60.985)', -1));
 gid | state |  county   |   city    |     name      | regionid 
-----+-------+-----------+-----------+---------------+----------
  29 | AK    | Anchorage | Anchorage | Turnagain Arm |   275783
(1 row)

gis=# \q
fmark@fmark-laptop:~$ 

This last stage can obviously be done from PHP by a simple query.

fmark