Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Shapefile data to determine neighborhood for a longitude/latitude

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

like image 237
Kunal Avatar asked Dec 17 '09 01:12

Kunal


People also ask

What type of data is latitude and longitude?

Location data are information about the geographic positions of devices (such as smartphones or tablets) or structures (such as buildings, attractions). The geographic positions of location data are called coordinates, and they are commonly expressed in Latitude and Longitude format.

What is location format?

The 3 most common location formats are: Decimal Degrees. = DD.DDDD *Paddling Map. format. Degrees Minutes & Seconds = DD MM SS.

What is longitude data?

The Latitude/Longitude Data Standard is a set of data elements that can be used for recording horizontal and vertical coordinates and associated metadata that define a point on the earth.


1 Answers

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.

like image 69
fmark Avatar answered Oct 19 '22 01:10

fmark