Ejemplo n.º 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()
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 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."
Ejemplo n.º 4
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')