Ejemplo n.º 1
0
def sense_list(request):
    coords = request.GET['bbox'].split(',')
    
    shp = ShpParser(None)
    p1 = shp.convert_point_to_OSGB36(coords[0], coords[1])
    p2 = shp.convert_point_to_OSGB36(coords[2], coords[3])
    OSGB36coors = [p1[0], p1[1], p2[0], p2[1]]
    bbox = Polygon.from_bbox(OSGB36coors)
    
    senses = MapPoints.objects.all()
    
    geojson_dict = {
        "type": "FeatureCollection",
        "features": [sense_to_geojson(sense) for sense in senses]
    }
    
    return HttpResponse(json.dumps(geojson_dict), content_type='application/json')
Ejemplo n.º 2
0
def neighbourhood(request):
    polys = []
    shp = ShpParser(None)
    
    try: # If lat, lng exist as parameters
        lat = request.GET['lat']
        lng = request.GET['lng']
        pt = shp.convert_point_to_OSGB36(lat, lng)
        point = Point(pt[0], pt[1], srid=27700)
        polys = Neighbourhood.objects.filter(poly__contains=point)
    except: # Otherwise
        try: # If the area name exists as a parameter
            area = request.GET['area']
            polys = Neighbourhood.objects.filter(name=area)
        except:
            pass
    
    if len(polys) > 1:
        print "Error: more than one polygons found."
    else:
        features = []
        for poly in polys:
            coords = []
            if poly.poly and isinstance(poly.poly, geos.MultiPolygon):
                for inside_poly in poly.poly:
                    inside_poly = inside_poly.simplify(100)
                    for pt_pair in inside_poly.coords:
                        for pt in pt_pair:
                            p = shp.convert_point_to_WGS84(pt[0], pt[1])
                            coords.append((p[1], p[0]))
            else:
                poly.poly = poly.poly.simplify(100)
                for pt_pair in poly.poly.coords:
                        for pt in pt_pair:
                            p = shp.convert_point_to_WGS84(pt[0], pt[1])
                            coords.append((p[1], p[0]))
                            
            features.append(neighbourhood_to_geojson(poly, coords))
            
        geojson_dict = {
            "type": "FeatureCollection",
            "features": features
        }
        return HttpResponse(json.dumps(geojson_dict), content_type='application/json')
Ejemplo n.º 3
0
def neighbourhood_list(request):
    coords = request.GET['bbox'].split(',')

    shp = ShpParser(None)
    p1 = shp.convert_point_to_OSGB36(float(coords[1]), float(coords[0]))
    p2 = shp.convert_point_to_OSGB36(float(coords[3]), float(coords[2]))

    bbox = Polygon.from_bbox(p1+p2)
    
    polys = Neighbourhood.objects.filter(poly__intersects=bbox)
    features = []
    # For all the polygons we've retrieved from the database (in OSGB36 format)
    for poly in polys:
        coords = []
        if poly.poly and isinstance(poly.poly, geos.MultiPolygon):
            for inside_poly in poly.poly:
                inside_poly = inside_poly.simplify(100)
                for pt_pair in inside_poly.coords:
                    for pt in pt_pair:
                        p = shp.convert_point_to_WGS84(pt[0], pt[1])
                        coords.append((p[1], p[0]))
        else:
            poly.poly = poly.poly.simplify(100)
            for pt_pair in poly.poly.coords:
                    for pt in pt_pair:
                        p = shp.convert_point_to_WGS84(pt[0], pt[1])
                        coords.append((p[1], p[0]))
                        
        features.append(neighbourhood_to_geojson(poly, coords))
    
    geojson_dict = {
        "type": "FeatureCollection",
        "features": features
    }
    
    return HttpResponse(json.dumps(geojson_dict), content_type='application/json')
Ejemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        super(MapPoints, self).__init__(*args, **kwargs)
        shp = ShpParser()
        tmp_point = shp.convert_point_to_OSGB36(self.lat, self.lon)
#        print "converted point: ", point
        self.pt = Point(float(tmp_point[0]), float(tmp_point[1]), srid=27700)
Ejemplo n.º 5
0
    def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
        # start drawing each block
        pil_map = Image.new("RGBA", (width, height), (255,255,255, 0))
        pil_draw = ImageDraw.Draw(pil_map)
        
        # return empty images
        if zoom < 11:
            return pil_map

        # first, figure out the bounding box of the tile we're rendering
        nw = self.layer.projection.projLocation(ModestMaps.Core.Point(xmin, ymin))
        se = self.layer.projection.projLocation(ModestMaps.Core.Point(xmax, ymax))
        max_lat = max(nw.lat, se.lat)
        min_lat = min(nw.lat, se.lat)
        max_lon = max(nw.lon, se.lon)
        min_lon = min(nw.lon, se.lon)
        
        # Converting polygon to OSGB36 in order to compare with the ones we have in
        # the database
        shp = ShpParser()
        min_p = shp.convert_point_to_OSGB36(min_lat, min_lon)
        max_p = shp.convert_point_to_OSGB36(max_lat, max_lon)
        
        bbox = Polygon.from_bbox((min_p[0], min_p[1], max_p[0], max_p[1]))
        
        # this obj is used to translate between lat/lon and pixel space
        bound1 = ModestMaps.Geo.Location(min_lat, min_lon)
        bound2 = ModestMaps.Geo.Location(max_lat, max_lon)
        mmap = ModestMaps.mapByExtentZoom(self.provider, bound1, bound2, zoom)

        neighbourhoods = False
        polys = None
        max_x = None
        min_x = None
        
        try:
            # If zoom < 15 we draw postcode polygons, otherwise neighbourhoods
            if zoom < 15 or self.type == "None":
                polys = Neighbourhood.objects.filter(poly__intersects=bbox)
                neighbourhoods = True
            else:
                polys = Postcode.objects.filter(poly__intersects=bbox)
                
            print "Painting", polys.count(), "blocks"
            
            # Have to find the city where the polygons belong in
            _city = None
            if self.type != "None":
                for poly in polys:
                    if _city is not None:
                        break
                    if poly.poly and isinstance(poly.poly, geos.MultiPolygon):
                        for inside_poly in poly.poly:
                            if _city is None:
                                # Find the city if we haven't found it yet
                                cities = Council.objects.filter(poly__intersects=inside_poly)
                                if len(cities) > 0:
                                    _city = cities[0].convexhull.name
                                    break
                    else: # Probably unneeded as eventually we only have Multipolygons in our db 
                        if _city is None:
                            # Find the city if we haven't found it yet
                            cities = Council.objects.filter(poly__intersects=inside_poly)
                            if len(cities) > 0:
                                _city = cities[0].convexhull.name
                                break
                        
            print "City:", _city
                
            if len(polys) > 0 and self.type != "None":        
                if neighbourhoods:
                    _name = _city+"_neighbourhood_max_"+self.type
                    max_x = float(Statistics.objects.filter(name=_name).all()[0].stat)
                    _name = _city+"_neighbourhood_min_"+self.type
                    min_x = float(Statistics.objects.filter(name=_name).all()[0].stat)
                else:
                    _name = _city+"_postcode_max_"+self.type
                    max_x = float(Statistics.objects.filter(name=_name).all()[0].stat)
                    _name = _city+"_postcode_min_"+self.type
                    min_x = float(Statistics.objects.filter(name=_name).all()[0].stat)
                        
                print "max:", max_x, "min:", min_x
                          
            # For all the polygons we've retrieved from the database (in OSGB36 format)
            for poly in polys:
                _city = None
                if poly.poly and isinstance(poly.poly, geos.MultiPolygon):
                    for inside_poly in poly.poly:
                        
                        if _city is None:
                            # Find the city if we haven't found it yet
                            cities = Council.objects.filter(poly__intersects=inside_poly)
                            if len(cities) > 0:
                                _city = cities[0].convexhull.name
    
                        self._paint_poly(inside_poly, shp, mmap, pil_draw, _city,
                                         max_x, min_x, neighbourhoods)
                else: # Probably unneeded as eventually we only have Multipolygons in our db 
                    if _city is None:
                        # Find the city if we haven't found it yet
                        cities = Council.objects.filter(poly__intersects=inside_poly)
                        if len(cities) > 0:
                            _city = cities[0].convexhull.name
                    self._paint_poly(poly.poly, shp, mmap, pil_draw, _city,
                                     max_x, min_x, neighbourhoods)
        except Exception, err:
            print err