def handle(self, *args, **options): constituencies = {} for c in Constituency.objects.all(): constituencies[reduceName(c.name)] = c for b in Boundary.objects.all(): b.delete() for f in ['boundaries/data/england.kml', 'boundaries/data/wales.kml', 'boundaries/data/scotland.kml', 'boundaries/data/northern_ireland.kml']: places = parse(f).getElementsByTagName("Placemark") for place in places: name = place.getElementsByTagName("name")[0].childNodes[0].toxml() v = [] for coords in place.getElementsByTagName("coordinates"): points = [] north = - google_dist south = google_dist east = - google_dist west = google_dist for coord in coords.childNodes[0].toxml().split(" "): s = coord.split(",") if len(s) == 3: x, y = [float(c) for c in coord.split(",")][:2] p = Point(x, y, srid=4326) p.transform(900913) gx, gy = p.coords if gy > north: north = gy if gy < south: south = gy if gx > east: east = gx if gx < west: west = gx points.append((gx, gy)) for z in range(maxZoom + 1): pixelsize2 = ((2 * google_dist / 256) / (2 ** z)) ** 2 u = [] previousX = 1e20 previousY = 1e20 for x, y in points: if z == maxZoom: u.append("(%f, %f)" % (x, y)) elif (x - previousX) ** 2 + (y - previousY) ** 2 > pixelsize2: u.append("(%f, %f)" % (x, y)) previousX, previousY = x, y if z != maxZoom and (previousX, previousY) != (x, y): u.append("(%f, %f)" % (x, y)) if len(u) > 3: constituency = constituencies[reduceName(name)] #Need to use this function due to slight name mismatches boundary="[%s]" % reduce(lambda x, y: "%s, %s" %(x, y), u).strip() b=Boundary(zoom = z, constituency = constituency, boundary=boundary, east = east, west = west, north = north, south = south) try: b.save() except: # print boundary pass
def test_as_dict(self): self.assertEqual( Boundary( set_id='foo', slug='bar', set_name='Foo', name='Bar', metadata={ 'baz': 'bzz', }, external_id=1, extent=[0, 0, 1, 1], centroid=Point(0, 1), start_date=date(2000, 1, 1), end_date=date(2010, 1, 1), ).as_dict(), { 'related': { 'boundary_set_url': '/boundary-sets/foo/', 'shape_url': '/boundaries/foo/bar/shape', 'simple_shape_url': '/boundaries/foo/bar/simple_shape', 'centroid_url': '/boundaries/foo/bar/centroid', 'boundaries_url': '/boundaries/foo/', }, 'boundary_set_name': 'Foo', 'name': 'Bar', 'metadata': { 'baz': 'bzz', }, 'external_id': 1, 'extent': [0, 0, 1, 1], 'centroid': { 'type': 'Point', 'coordinates': (0.0, 1.0), }, 'start_date': '2000-01-01', 'end_date': '2010-01-01', }) self.assertEqual( Boundary( set_id='foo', slug='bar', ).as_dict(), { 'related': { 'boundary_set_url': '/boundary-sets/foo/', 'shape_url': '/boundaries/foo/bar/shape', 'simple_shape_url': '/boundaries/foo/bar/simple_shape', 'centroid_url': '/boundaries/foo/bar/centroid', 'boundaries_url': '/boundaries/foo/', }, 'boundary_set_name': '', 'name': '', 'metadata': {}, 'external_id': '', 'extent': None, 'centroid': None, 'start_date': None, 'end_date': None, })
def test_unary_union(self): boundary = Boundary( shape='MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)))') boundary.unary_union( Geometry(OGRGeometry('MULTIPOLYGON (((0 0,5 0,5 5,0 0)))'))) self.assertEqual(boundary.shape.ogr.wkt, 'MULTIPOLYGON (((5 5,5 0,0 0,0 5,2.5 5.0001,5 5)))') self.assertEqual(boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((5 5,5 0,0 0,0 5,5 5)))')
def test_merge(self): boundary = Boundary( shape='MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)))', simple_shape='MULTIPOLYGON (((0 0,0 5,5 5,0 0)))') boundary.merge( Geometry( OGRGeometry('MULTIPOLYGON (((0 0,5 0,5.0001 2.5,5 5,0 0)))'))) self.assertEqual( boundary.shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)),((0 0,5 0,5.0001 2.5,5 5,0 0)))' ) self.assertEqual( boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,5 5,0 0)),((0 0,5 0,5 5,0 0)))')
def test_get_dicts(self): boundaries = [ ('bar', 'foo', 'Bar', 'Foo', 1), ('bzz', 'baz', 'Bzz', 'Baz', 2), ] self.assertEqual(Boundary.get_dicts(boundaries), [ { 'url': '/boundaries/foo/bar/', 'name': 'Bar', 'related': { 'boundary_set_url': '/boundary-sets/foo/', }, 'boundary_set_name': 'Foo', 'external_id': 1, }, { 'url': '/boundaries/baz/bzz/', 'name': 'Bzz', 'related': { 'boundary_set_url': '/boundary-sets/baz/', }, 'boundary_set_name': 'Baz', 'external_id': 2, }, ])
def handle(self, *args, **options): filename = os.path.join(settings.KMLDIR, "territory_boundaries.kml") # Parse kml_str kml = parseString(open(filename).read()) added = 0 failed = 0 print "Loading boundaries from: %s" % filename for placemark in kml.getElementsByTagName("Placemark"): polygon = placemark.getElementsByTagName("Polygon") try: kml_str = polygon[0].toxml() sys.stdout.write('.') except: sys.stdout.write('!') failed += 1 continue added += 1 b = Boundary() b.set_poly_from_kml_str(kml_str) b.boundary_type_id = 5 b.save() print "\nFinished!" print "Successfully added: %d" % added print "Failed to add: %d" % failed
def get_boundaries(self, sets=None): r = {'boundaries_concordance': [], 'boundaries_centroid': []} concordances = PostcodeConcordance.objects.filter( code=self.code).values_list('boundary', flat=True) if sets: concordances = [ boundary for boundary in concordances if boundary.split('/')[0] in sets ] concordance_sets = set() if concordances: q = ((models.Q(set=concordance.split('/')[0]) & models.Q(slug=concordance.split('/')[1])) for concordance in concordances) boundaries = Boundary.objects.filter(reduce(lambda a, b: a | b, q)) boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries) boundaries = Boundary.get_dicts(boundaries) r['boundaries_concordance'] = boundaries for boundary in boundaries: concordance_sets.add(boundary['related']['boundary_set_url']) if self.centroid: q = models.Q(shape__contains=self.centroid) if sets: q &= models.Q(set__in=sets) boundaries = Boundary.objects.filter(q) boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries) boundaries = Boundary.get_dicts(boundaries) r['boundaries_centroid'] = [ boundary for boundary in boundaries if boundary['related'] ['boundary_set_url'] not in concordance_sets ] return r
def kml(args): """Load boundary polygons from a KML.""" import sys import os from boundaries.models import Boundary, BoundaryType, BoundariesRelated from xml.dom.minidom import parseString import settings #b = Boundary.objects.all()[0] file_path = os.path.join(settings.KMLDIR, args.kmlfilename) boundary_type_id = args.boundary_type_id # file exists? if not os.path.exists(file_path): print "File not found: %s" % file_path sys.exit() # boundary_type id valid? try: boundary_type_id = BoundaryType.objects.get(id=boundary_type_id).id except: print "Invalid Boundary Type ID: %d" % boundary_type_id sys.exit() print file_path, boundary_type_id # Parse kml_str kml = parseString(open(file_path).read()) added = 0 failed = 0 print "Loading boundaries from: %s" % file_path for placemark in kml.getElementsByTagName("Placemark"): polygon = placemark.getElementsByTagName("Polygon") try: kml_str = polygon[0].toxml() sys.stdout.write('.') except: sys.stdout.write('!') failed += 1 continue added += 1 b = Boundary() b.set_poly_from_kml_str(kml_str) b.boundary_type_id = boundary_type_id b.save() print "\nFinished!" print "Successfully added: %d" % added if failed: print "Failed to add: %d" % failed
def get_boundaries(self, sets=None): r = { 'boundaries_concordance': [], 'boundaries_centroid': [] } concordances = PostcodeConcordance.objects.filter(code=self.code).values_list('boundary', flat=True) if sets: concordances = [boundary for boundary in concordances if boundary.split('/')[0] in sets] concordance_sets = set() if concordances: q = ((models.Q(set=concordance.split('/')[0]) & models.Q(slug=concordance.split('/')[1])) for concordance in concordances) boundaries = Boundary.objects.filter(reduce(lambda a, b: a | b, q)) boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries) boundaries = Boundary.get_dicts(boundaries) r['boundaries_concordance'] = boundaries for boundary in boundaries: concordance_sets.add(boundary['related']['boundary_set_url']) if self.centroid: q = models.Q(shape__contains=self.centroid) if sets: q &= models.Q(set__in=sets) boundaries = Boundary.objects.filter(q) boundaries = Boundary.prepare_queryset_for_get_dicts(boundaries) boundaries = Boundary.get_dicts(boundaries) r['boundaries_centroid'] = [boundary for boundary in boundaries if boundary['related']['boundary_set_url'] not in concordance_sets] return r
def test_prepare_queryset_for_get_dicts(self): Boundary.objects.create( slug='bar', set=BoundarySet(slug='foo'), name='Bar', set_name='Foo', external_id=1, shape=MultiPolygon(()), simple_shape=MultiPolygon(()), ) # Coerce the django.contrib.gis.db.models.query.GeoValuesListQuerySet. self.assertEqual(list(Boundary.prepare_queryset_for_get_dicts(Boundary.objects)), [ ('bar', 'foo', 'Bar', 'Foo', '1'), ])
def test_prepare_queryset_for_get_dicts(self): geom = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,0 0)))') Boundary.objects.create( slug='bar', set=BoundarySet(slug='foo'), name='Bar', set_name='Foo', external_id=1, shape=geom, simple_shape=geom, ) # Coerce the django.contrib.gis.db.models.query.GeoValuesListQuerySet. self.assertEqual(list(Boundary.prepare_queryset_for_get_dicts(Boundary.objects)), [ ('bar', 'foo', 'Bar', 'Foo', '1'), ])
def test_cascaded_union(self): boundary = Boundary(shape='MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)))') boundary.cascaded_union(Geometry(OGRGeometry('MULTIPOLYGON (((0 0,5 0,5 5,0 0)))'))) self.assertEqual(boundary.shape.ogr.wkt, 'MULTIPOLYGON (((5 5,5 0,0 0,0 5,2.5 5.0001,5 5)))') self.assertEqual(boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((5 5,5 0,0 0,0 5,5 5)))')
def test_merge(self): boundary = Boundary(shape='MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)))', simple_shape='MULTIPOLYGON (((0 0,0 5,5 5,0 0)))') boundary.merge(Geometry(OGRGeometry('MULTIPOLYGON (((0 0,5 0,5.0001 2.5,5 5,0 0)))'))) self.assertEqual(boundary.shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,2.5 5.0001,5 5,0 0)),((0 0,5 0,5.0001 2.5,5 5,0 0)))') self.assertEqual(boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,5 5,0 0)),((0 0,5 0,5 5,0 0)))')
def handle(self, *args, **options): constituencies = {} for c in Constituency.objects.all(): constituencies[reduceName(c.name)] = c for b in Boundary.objects.all(): b.delete() print constituencies.keys() for f in [ 'boundaries/data/england.kml', 'boundaries/data/wales.kml', 'boundaries/data/scotland.kml', 'boundaries/data/northern_ireland.kml' ]: print f places = parse(f).getElementsByTagName("Placemark") for place in places: name = place.getElementsByTagName( "name")[0].childNodes[0].toxml() print name v = [] for coords in place.getElementsByTagName("coordinates"): points = [] north = -google_dist south = google_dist east = -google_dist west = google_dist for coord in coords.childNodes[0].toxml().split(" "): s = coord.split(",") if len(s) == 3: x, y = [float(c) for c in coord.split(",")][:2] p = Point(x, y, srid=4326) p.transform(900913) gx, gy = p.coords if gy > north: north = gy if gy < south: south = gy if gx > east: east = gx if gx < west: west = gx points.append((gx, gy)) for z in range(maxZoom + 1): pixelsize2 = ((2 * google_dist / 256) / (2**z))**2 u = [] previousX = 1e20 previousY = 1e20 for x, y in points: if z == maxZoom: u.append("(%f, %f)" % (x, y)) elif (x - previousX)**2 + ( y - previousY)**2 > pixelsize2: u.append("(%f, %f)" % (x, y)) previousX, previousY = x, y if z != maxZoom and (previousX, previousY) != (x, y): u.append("(%f, %f)" % (x, y)) if len(u) > 3: constituency = constituencies[reduceName( name )] #Need to use this function due to slight name mismatches boundary = "[%s]" % reduce( lambda x, y: "%s, %s" % (x, y), u).strip() b = Boundary(zoom=z, constituency=constituency, boundary=boundary, east=east, west=west, north=north, south=south) try: b.save() except: print boundary if len(v) >= 1: print "'%s'" % name
def test_boundary_set_name(self): self.assertEqual(Boundary(set_name='Foo').boundary_set_name, 'Foo')
def test_boundary_set(self): self.assertEqual(Boundary(set=BoundarySet(slug='foo')).boundary_set, 'foo')
def test_get_absolute_url(self): self.assertEqual(Boundary(set_id='foo', slug='bar').get_absolute_url(), '/boundaries/foo/bar/')
def test___str__(self): self.assertEqual(str(Boundary(set_name='Foo', name='Bar')), 'Bar (Foo)')