def test_inside_poly3(): polya=Polygon(vvector([ Vertex(0,0),Vertex(10,0),Vertex(10,10),Vertex(5,5),Vertex(0,10)])) assert polya.is_inside(Vertex(7,5)) assert polya.is_inside(Vertex(5,5)) #on edge assert polya.is_inside(Vertex(3,5)) assert not polya.is_inside(Vertex(5,7))
def test_inside_poly1(): polya=Polygon(vvector([ Vertex(0,0),Vertex(10,0),Vertex(10,10),Vertex(0,10)])) assert not polya.is_inside(Vertex(5,11)) assert polya.is_inside(Vertex(5,5)) assert polya.is_inside(Vertex(0,5)) assert polya.is_inside(Vertex(5,10)) assert not polya.is_inside(Vertex(-1,5)) assert not polya.is_inside(Vertex(11,5))
def get_weather(lat, lon): zoomlevel = 13 px, py = mapper.latlon2merc((lat, lon), zoomlevel) w = Weather() areas = weather_chart_areas.get_areas() insides = [] dists = [] for name, area in areas.items(): poly_coords = [] centerx = 0.0 centery = 0.0 for coord in area: x, y = mapper.latlon2merc(coord, zoomlevel) centerx += x + 0.0 centery += y + 0.0 poly_coords.append(Vertex(int(x), int(y))) if len(poly_coords) < 3: print "Weather area %s has few points: %s " % (name, area) continue centerx /= len(poly_coords) centery /= len(poly_coords) cdist = (centerx - px)**2 + (centery - py)**2 poly = Polygon(vvector(poly_coords)) if poly.is_inside(Vertex(int(px), int(py))): cdist *= 1e-3 insides.append((cdist, name, area)) #print "insides:",insides insides.sort() dist, name, area = insides[0] if dist > 20000**2: return None w.weather_area = name mainarea, rest = name.split("_") part = rest[:-2] seg = rest[-2:] try: fc = get_parsed_weather() except Exception, cause: print "Couldn't fetch weather: ", cause #there's no weather service at certain times. return None
def get_weather(lat,lon): zoomlevel=13 px,py=mapper.latlon2merc((lat,lon),zoomlevel) w=Weather() areas=weather_chart_areas.get_areas() insides=[] dists=[] for name,area in areas.items(): poly_coords=[] centerx=0.0 centery=0.0 for coord in area: x,y=mapper.latlon2merc(coord,zoomlevel) centerx+=x+0.0 centery+=y+0.0 poly_coords.append(Vertex(int(x),int(y))) if len(poly_coords)<3: print "Weather area %s has few points: %s "%(name,area) continue centerx/=len(poly_coords) centery/=len(poly_coords) cdist=(centerx-px)**2+(centery-py)**2 poly=Polygon(vvector(poly_coords)) if poly.is_inside(Vertex(int(px),int(py))): cdist*=1e-3 insides.append((cdist,name,area)) #print "insides:",insides insides.sort() dist,name,area=insides[0] if dist>20000**2: return None w.weather_area=name mainarea,rest=name.split("_") part=rest[:-2] seg=rest[-2:] try: fc=get_parsed_weather() except Exception,cause: print "Couldn't fetch weather: ",cause #there's no weather service at certain times. return None
def get_polygons_around(lat,lon,polys): zoomlevel=13 px,py=mapper.latlon2merc((lat,lon),zoomlevel) insides=[] for space in polys: poly_coords=[] for coord in space['points']: x,y=mapper.latlon2merc(mapper.from_str(coord),zoomlevel) poly_coords.append(Vertex(int(x),int(y))) if len(poly_coords)<3: #print "Space %s has few points: %s "%(space['name'],space['points']) continue poly=Polygon(vvector(poly_coords)) #print "Checking if inside poly:",space if poly.is_inside(Vertex(int(px),int(py))): insides.append(space) #print "Is inside" else: pass#print "Is NOT inside" return insides
def test_inside_poly2(): polya=Polygon(vvector([ Vertex(0,0),Vertex(0,5),Vertex(10,0),Vertex(10,10),Vertex(0,10)])) assert polya.is_inside(Vertex(5,5))