[GIS] Finding all street names or addresses within polygon

geojsonpoint-in-polygon

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.

Best Answer

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.