4

I have a geojson list of lat/long coordinates that make up a custom polygon of a section of my county. Ideally I would like to be able to find all the addresses that are within the polygon boundaries.

My county GIS department provides for free a geojson list of all the polygon coordinates that make up every single building in the county.

Is there a way I can use these two files together to determine all the addresses inside the custom polygon?

If this is not really possible, another possibility is to list all the street names within the polygon.

The ultimate goal is to provide a list of all the names, phone numbers & addresses in the polygon, all done programmatically.

1

You could use a spatial database (like PostGIS). First, you put all your addresses in a table "addresses" with a column "Point" of type Point and a column "Address".

CREATE TABLE addresses (Point Point, Address Varchar(50));

Then you put your polygon in another table, let's call it "poly". It can have a column "Polygon" of type Polygon and another column "PolygonId".

CREATE TABLE poly (Polygon Polygon, PolygonId numeric);

Let's say you inserted your Polygon with the PolygonId=1.

Then you can use the following request:

SELECT ad.Address
FROM ad addresses, pol poly
WHERE contains(pol.Polygon, ad.Point) = 1 AND
pol.PolygonId=1;

I did not cover the insertion of the data into the data base, I would do it with a script (I use Node JS) but there may be other ways. If you want to do all of that programmatically, you can easily implement a script that triggers the SQL request. I would use NodeJS with the pg module to communicate with a PostgreSQL + PostGIS database but there are other stacks ;-)

Edit: the "Contains" statement is not vendor specific, for postgis, you should use ST_Contains.

  • This is a fantastic solution, however...I don't have the addresses. I only have the polygon coords of where the addresses are. I suppose I could do some sort of batch reverse geocoding for all the addresses but it would be a hassle and take a long time. – Jared Eitnier Oct 13 '14 at 14:09
  • Ooh tough one. Maybe you could extract one pair of longitude and latitude from each building you get in the request and use the reverse geocoding of the Google Maps API ? developers.google.com/maps/documentation/geocoding – syldor Oct 13 '14 at 16:39
  • Yeah that was my first thought. I'll work on that idea and report back when I have something. – Jared Eitnier Oct 13 '14 at 17:01
0

If you imported osm file into a data base then you can use this Query:

SELECT distinct polygon.name,line.name 
FROM planet_osm_line AS line CROSS JOIN planet_osm_polygon AS polygon 
WHERE polygon.admin_level = '8' 
AND ST_Contains(polygon.way, line.way) 
AND line.highway in ('primary', 'secondary', 'tertiary', 'road', 'residential','motorway', 'trunk', 'unclassified') 
AND polygon.name like 'K%nigstein'
ORDER BY line.name
  • 1
    Please make an effort to write properly, you really isn't that hard to write, nor is I. I have a broken arm, and I can do it. – John Powell Nov 20 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.