I'm trying to test if a point is inside a polygon using this example:
https://stackoverflow.com/questions/20776205/point-in-polygon-with-geojson-in-python
However, my output says that none of the points is inside any of the polygons. Since the points are defined on the map of NYC, and my polygons define community districts of NYC, this doesn't make sense.
This is my full code:
import urllib2, json, csv
import numpy
from shapely.geometry import shape, Point
def readJson(url):
"""
Returns a json file specified in @url.
"""
response = urllib2.urlopen(url)
return json.loads(response.read())
def readCSV(url):
"""
Returns a csv file specified in @url.
"""
response = urllib2.urlopen(url)
return csv.DictReader(response, delimiter=',')
def getRegions():
"""
Returns a dictionary formed by the id of a region and its coordinates.
"""
dict = {}
# (longitude, latitude)
url = "https://nycdatastables.s3.amazonaws.com/2013-08-19T18:22:23.125Z/community-districts-polygon.geojson"
data = readJson(url)
for district in data['features']:
dict[district['id']] = district['geometry']
return dict
def getPOIs():
"""
Returns a list of tuples of POIs lat/long coordinates.
"""
urls = ["https://nycdatastables.s3.amazonaws.com/2013-06-04T18:02:56.019Z/museums-and-galleries-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-12-16T21:49:55.716Z/nyc-parking-facilities-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-20T16:06:05.136Z/mapped-in-ny-companies-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-11T18:59:27.269Z/nyc-public-school-locations-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-07-29T15:49:03.498Z/nyc-private-school-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-07-01T16:25:00.297Z/nyc-special-education-school-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-05T14:35:56.387Z/basic-description-of-colleges-and-universities-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-05T20:25:17.301Z/operating-sidewalk-cafes-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-04T14:40:48.764Z/community-health-centers-results.csv",
"http://data.nycprepared.org/ar/dataset/dycd-after-school-programs-housing/resource/d2306a8f-59d1-4cb0-b527-ba44ca8eec3a",
"http://data.nycprepared.org/ar/dataset/dycd-after-school-programs-family-support-programs-for-seniors/resource/493f52a4-0a49-4f5f-8937-78e69fb77852",
"https://nycdatastables.s3.amazonaws.com/2013-07-02T15:29:20.692Z/agency-service-center-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-13T18:39:44.536Z/nyc-2012-farmers-market-list-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-10-18T21:14:52.348Z/nyc-grocery-stores-final.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-18T14:29:37.626Z/subway-entrances-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-04T17:58:59.335Z/map-of-monuments-results.csv",
"https://nycdatastables.s3.amazonaws.com/2013-06-18T20:17:34.010Z/nyc-landmarks-results.csv"]
POIs = []
for url in urls:
csv = readCSV(url)
print url
for line in csv:
latitude = line.get('latitude', None)
longitude = line.get('longitude', None)
if latitude is not None and longitude is not None:
POIs.append((float(longitude), float(latitude)))
return POIs
def POIsInRegion(regions, POIs):
"""
Returns a dictionary formed by the id of a region and the number of POIs that falls in
this region.
"""
dict = {}
for key, value in regions.iteritems():
dict[key] = 0
polygon = shape(value)
# print value
for p in POIs:
point = Point(p[0], p[1])
# print point
if polygon.contains(point):
print True, point
dict[key] += 1
return dict
if __name__ == '__main__':
# Geographical Features
regions_bbox = getRegions()
regions_number = len(regions_bbox)
print "Regions: ", regions_number
print "Reading POIs..."
POIs = getPOIs()
print "Done Reading ", len(POIs), " POIs"
print "Calculating POIs per Region"
POIsPerRegion = POIsInRegion(regions_bbox, POIs)
print POIsPerRegion
All data are valid and obtained at http://nycprepared.org.
Example:
First polygon (related to this data -- map can be view):
{u'type': u'Polygon', u'coordinates': [[[-74.01115034338935, 40.725777216880076], [-74.01081238260726, 40.72578980255575], [-73.99931241700145, 40.71755024177738], [-74.0005783921612, 40.71557090292421], [-74.0004546306596, 40.71436504759887], [-74.0009317433238, 40.71326325304528], [-74.00086781464309, 40.71156815886214], [-74.00050986097233, 40.710984270453366], [-74.0014714023208, 40.70974655432324], [-73.99919451174895, 40.70794737635146], [-74.00118685262828, 40.70685982577175], [-74.00098276757775, 40.706416712992926], [-74.00140666691954, 40.70617938802063], [-74.0005829704798, 40.70543393364888], [-74.00143661245443, 40.70487217770522], [-74.00206142563805, 40.70541700463557], [-74.00256965059467, 40.70552357952349], [-74.00268512768794, 40.70544924811865], [-74.00197070463986, 40.704737867482024], [-74.00217184875859, 40.70460659646367], [-74.00243071075865, 40.70467785800434], [-74.00238670806719, 40.70479566367446], [-74.00332307270787, 40.70562859522005], [-74.00363187169326, 40.70542980675174], [-74.00287843069457, 40.70483135798975], [-74.00297246475286, 40.70476649143739], [-74.00364446183866, 40.7054216846388], [-74.00438344417225, 40.70511325985482], [-74.00426687037428, 40.70501166020203], [-74.00459003883672, 40.70478775294501], [-74.00349632042324, 40.7037967692677], [-74.0037198704304, 40.703654481944646], [-74.00480628900745, 40.70464206905116], [-74.00518805133365, 40.70442053569055], [-74.00492580564406, 40.70421180838463], [-74.00521160900402, 40.704405532235306], [-74.00535027224755, 40.70431007996152], [-74.00415742154165, 40.70323163208721], [-74.004272587521, 40.703015666411474], [-74.00559242609006, 40.70408256373161], [-74.00601862790923, 40.703873152890765], [-74.00614010577931, 40.70397866027174], [-74.00659591913875, 40.70368590245182], [-74.00530412596078, 40.70255986563303], [-74.00549427805095, 40.70243138095038], [-74.00553546138175, 40.70264552110683], [-74.00567522315887, 40.702584071694936], [-74.00678502257081, 40.70356812690006], [-74.00930907655481, 40.70195460302253], [-74.00869964313567, 40.70113986754221], [-74.00775549150639, 40.70154800167179], [-74.00759136204219, 40.70132808104811], [-74.00853611510774, 40.70091969574503], [-74.008349275591, 40.70066934838819], [-74.00862036163862, 40.700552163210666], [-74.00960052263287, 40.701865481917835], [-74.01116664217923, 40.70147720220825], [-74.01109588602678, 40.70127499934884], [-74.01128983905524, 40.701249982244235], [-74.01079959311005, 40.70020721300695], [-74.01115033556837, 40.70089840010092], [-74.01135836291354, 40.70085195863632], [-74.01133409344382, 40.70056835368232], [-74.0115029114371, 40.70081505897418], [-74.01172926817475, 40.70077011805966], [-74.01163147918649, 40.7004582182481], [-74.01185378549589, 40.700738921450146], [-74.01208668214691, 40.700681308739156], [-74.01185672623029, 40.70010710340832], [-74.01196196116918, 40.70003754559543], [-74.01239160032284, 40.700620528094], [-74.01259392327509, 40.70057083347241], [-74.01261382087364, 40.70010826581106], [-74.01290881859993, 40.7005236116934], [-74.01310747847371, 40.700507452180624], [-74.01301189331895, 40.69981825663114], [-74.01311303826294, 40.69977416488007], [-74.01327349549122, 40.70049687022768], [-74.01348560832739, 40.700478222908394], [-74.01353285084137, 40.70010816142314], [-74.01367479248758, 40.70012582145088], [-74.01353047796475, 40.70031110034644], [-74.01355826959487, 40.70093987083052], [-74.01395152690475, 40.70099098246003], [-74.01422063261519, 40.700111490863044], [-74.01415238853336, 40.70057786578343], [-74.01428920741134, 40.70064414476502], [-74.01467120058032, 40.700210400694424], [-74.01432384363017, 40.700661304127095], [-74.01464323168875, 40.70081730432518], [-74.01509832270125, 40.700334265523416], [-74.01467365628476, 40.70083460852581], [-74.01502113661678, 40.70099250401337], [-74.015142318121, 40.70084289235424], [-74.01714508062658, 40.70270534997615], [-74.01764797572895, 40.70349459987939], [-74.01757035706059, 40.70418388098645], [-74.01779454520279, 40.704234897096185], [-74.01781758697946, 40.703937922496536], [-74.01795474048662, 40.70397578560846], [-74.01784441106724, 40.704242039937895], [-74.01844770243773, 40.704162168231505], [-74.01866830771891, 40.704435239616195], [-74.01748086152573, 40.70466748769614], [-74.01811296129115, 40.70455372748716], [-74.01888746649365, 40.70472914883734], [-74.0193425462446, 40.706093672024494], [-74.01912275963412, 40.706975814337405], [-74.01863058372699, 40.707139715269264], [-74.01884889814508, 40.706970312350194], [-74.01853513269668, 40.706918322014054], [-74.01819029325691, 40.7078216261098], [-74.01885500911966, 40.708047534447395], [-74.01777014254503, 40.712834574789106], [-74.01780037822344, 40.71234512415862], [-74.01662424491339, 40.71215731899518], [-74.01661840078305, 40.71275615002965], [-74.0163200662758, 40.713407982478245], [-74.01754443334657, 40.71361122266423], [-74.01772496219294, 40.7130701816227], [-74.01671018605823, 40.71862417605797], [-74.01321322719193, 40.71831571989575], [-74.0129144874633, 40.7197082932253], [-74.01344694049585, 40.71978201978623], [-74.01338906903445, 40.72004260727319], [-74.01452415856502, 40.720187397445684], [-74.01452279056075, 40.72053205032773], [-74.01296558526825, 40.720328675587126], [-74.01286909138038, 40.720833752211675], [-74.0132393733702, 40.720887986728066], [-74.01308010145637, 40.72154580719522], [-74.01253875807262, 40.72150317657635], [-74.01165326569942, 40.725871761851344], [-74.01516324118423, 40.72631971193678], [-74.01517084085862, 40.726579390657555], [-74.01145763103551, 40.72615568926202], [-74.01115034338935, 40.725777216880076]]]}
First points (related to this data -- map can be view):
POINT (-74.01375579499995 40.70381655000006)
POINT (-74.06303178799993 40.61512117100006)
POINT (-73.94729768499991 40.83385383400008)
POINT (-73.97810302099992 40.76162530600004)
POINT (-74.03968483799991 40.69905659600005)
POINT (-73.97364816499993 40.78082656700008)
POINT (-74.00701187899995 40.72352692500004)
POINT (-73.96597045099992 40.76882456300007)
POINT (-73.99963036799994 40.72112778300004)
POINT (-73.96428395699991 40.76983411600008)
POINT (-73.94654590499994 40.83358261600006)
POINT (-73.80557949899992 40.87177865100006)
POINT (-73.82489046299992 40.76288666600004)
POINT (-73.8797487409999 40.87823677400007)
POINT (-73.91975017699991 40.83103011300005)
POINT (-73.94401601399994 40.67450733000004)
POINT (-73.99240540399995 40.69480330800008)
POINT (-73.96358506799993 40.67108354500004)
POINT (-74.00731553399993 40.74789793400004)
POINT (-73.97727392099995 40.78587125400009)
POINT (-73.99883836099991 40.72074980300005)
POINT (-73.93173408699994 40.86492396800008)
POINT (-73.97985598399993 40.57526357600005)
POINT (-73.95778161999993 40.78432607400003)
POINT (-73.97304190099993 40.76210730600008)
POINT (-73.94649318099994 40.83330244300004)
POINT (-74.00613537699991 40.74789526000006)
POINT (-73.9873186399999 40.75750141900005)
POINT (-74.00290417199994 40.72245152200009)
POINT (-73.92383914699991 40.86713125400007)
POINT (-73.99272306099994 40.72496003700007)
POINT (-73.9513676009999 40.79309925400008)
POINT (-74.04132161699994 40.69820788700008)
POINT (-74.01133926299991 40.70339960900009)
POINT (-73.96708040599992 40.77105498700007)
POINT (-74.07387774199992 40.61516555800006)
POINT (-73.96174203899994 40.77916597400008)
POINT (-73.94306846199993 40.77611378300008)
POINT (-73.91397962899993 40.85874750300007)
POINT (-74.03231077699991 40.60900900100006)
POINT (-73.94682282199994 40.83345248000006)
POINT (-73.89098784299995 40.87835819800006)
POINT (-74.01808694199991 40.67557226800005)
POINT (-73.98383672499995 40.75602148500008)
Result:
Calculating POIs per Region
True POINT (-73.99386877199993 40.73798709800008)
0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0, 40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0, 50: 0, 51: 0, 52: 0, 53: 0, 54: 0, 55: 0, 56: 0, 57: 0, 58: 0, 59: 0, 60: 0, 61: 0, 62: 0, 63: 0, 64: 0, 65: 0, 66: 0, 67: 0, 68: 0, 69: 0, 70: 0, 71: 0, 72: 0, 73: 0, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 0, 80: 0, 81: 0, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 0, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 1, 98: 0, 99: 0, 100: 0, 101: 0, 102: 0, 103: 0, 104: 0, 105: 0, 106: 0, 107: 0, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 0, 116: 0, 117: 0, 118: 0, 119: 0, 120: 0, 121: 0, 122: 0, 123: 0, 124: 0, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 0, 132: 0, 133: 0, 134: 0, 135: 0, 136: 0, 137: 0, 138: 0, 139: 0, 140: 0, 141: 0, 142: 0, 143: 0, 144: 0, 145: 0, 146: 0, 147: 0, 148: 0}
ONE SINGLE TRUE printed. It doesn't make sense.
For instance, this point, from the first .csv:
Is clearly inside this polygon:
And it's not printed as `True!
I don't know what I'm missing.
POIsInRegion
, try to insert aprint True
in blockif polygon.contains(point):
and comment bothprint value
andprint point
. You will see it printsTrue
. Your error is not in shapely but in your dict manipulation IMO