Exemple #1
0
def store(row):
    shp = ShpParser(None)
    entry = []
    for col in row:
        entry.append(col)
        
    pts = shp.convert_point_to_WGS84(entry[3], entry[4])
    point = Point(float(entry[3]), float(entry[4]), srid=27700)
    neighbourhoods = Neighbourhood.objects.filter(poly__contains=point)
    
    nb = None
    
    if len(neighbourhoods) >= 1:
        nb = neighbourhoods[0]
    # Else the crime is not covered by any neighbouhood polygon (e.g. River thames)
    
    # Convert to format YYYY-MM-DD by replacing all occurences of "/" with "-".
    d = entry[0]
    d = d.replace('/', '-')
    d += "-01"

    c = Crimepoint(crimecat=entry[6], pt = Point(pts[1], pts[0]), 
                   streetname=entry[5], month=d, 
                   neighbourhood=nb)
    c.save()
Exemple #2
0
    def store(self, city=None):
        c = '' if city is None else str(city)
        allfiles = [f for f in os.listdir(config.filteredDir) 
                    if os.path.isfile(os.path.join(config.filteredDir,f)) and
                    (".csv" in f) and (c in f)]
        
        print "Storing crimes..."
        print "c:", c
        print "allfiles:", allfiles
        count = 0;
        
        for f in allfiles:
            ifile = open(config.filteredDir+f, "rb")
            reader = csv.reader(ifile)
            shp = ShpParser(None)
            print "Storing from file:", config.filteredDir+f
            for row in reader:
                entry = []
                for col in row:
                    entry.append(col)
                    
                assert(len(entry) == 8)
                
                pts = shp.convert_point_to_WGS84(entry[3], entry[4])
                point = Point(float(entry[3]), float(entry[4]), srid=27700)
                
                neighbourhoods = Neighbourhood.objects.filter(poly__intersects=point)
                
                nb = None
                
                if len(neighbourhoods) == 0:
                    # Else the crime is not covered by any neighbouhood polygon (e.g. River thames)
                    print "No neighbourhood found for point:", pts, "OSGB36:", point
                elif len(neighbourhoods) == 1:
                    nb = neighbourhoods[0]
                else:
#                    print "More than one polygons (",len(neighbourhoods),")  found for point:", pts, "OSGB36:", point
                    nb = neighbourhoods[0]
                
                # Convert to format YYYY-MM-DD by replacing all occurences of "/" with "-".
                d = entry[0]
                d = d.replace('/', '-')
                d += "-01"

                c = Crimepoint(crimecat=entry[6], pt = point, 
                               streetname=entry[5], month=d, 
                               neighbourhood=nb)
                c.save()
                    
                count += 1
            
        print "Stored", count, "crimes."
Exemple #3
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')
Exemple #4
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')
Exemple #5
0
 def extractAll(self):
     shp = ShpParser(config.poly_dir)
     for city in config.allcities.keys():
         # Greater London is a special case as it has 32 councils
         if city != "Greater London":
             shp.parse(config.boundaries_file, city, False)
         
     shp = ShpParser(config.greater_london_out_dir)
     for borough in config.greater_london:
         shp.parse(config.boundaries_file, borough, False)
         
     pts = convexhull.merge(config.greater_london_out_dir)
     c = convexhull.convexHull(pts)
     shp.write('Greater London_OSGB36_polygon.csv', c)
Exemple #6
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')
Exemple #7
0
 def extractConvexhulls(self):
     shp = ShpParser(config.convexhulls_dir)
     
     allfiles = [f for f in os.listdir(config.poly_dir) 
                 if os.path.isfile(os.path.join(config.poly_dir,f)) and
                 "OSGB36_polygon.csv" in f]
     
     for fn in allfiles:
         pts = convexhull.loadCSV(config.poly_dir+fn)
         pts = shp.convert_list_to_WGS84(pts)
         c = convexhull.convexHull(pts)
         shp.write(fn[0:-18]+"WGS84_convexhull.csv", c)
     
     name = "Greater London_OSGB36_polygon.csv"
     pts = convexhull.loadCSV(config.greater_london_out_dir+name)
     pts = shp.convert_list_to_WGS84(pts)
     shp.write(name[0:-18]+"WGS84_convexhull.csv", pts)
Exemple #8
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)
Exemple #9
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
Exemple #10
0
 def extractOne(self, cityName):
     shp = ShpParser(config.poly_dir)
     shp.parse(config.boundaries_file, cityName, False)
     shp.parse(config.boundaries_file, cityName, True)