Beispiel #1
0
    def test_kwargs_from_temba(self):
        geometry = TembaGeometry.create(type='MultiPolygon', coordinates=['COORDINATES'])
        country = TembaBoundary.create(boundary='R12345', name='Nigeria', parent=None, level=0, geometry=geometry)

        kwargs = Boundary.kwargs_from_temba(self.nigeria, country)
        self.assertEqual(kwargs, dict(org=self.nigeria, osm_id="R12345", name="Nigeria", level=0, parent=None,
                                      geometry=json.dumps(dict(type='MultiPolygon', coordinates=['COORDINATES']))))

        # try creating an object from the kwargs
        country_boundary = Boundary.objects.create(**kwargs)

        state = TembaBoundary.create(boundary='R23456', name='Lagos', parent="R12345", level=1, geometry=geometry)
        kwargs = Boundary.kwargs_from_temba(self.nigeria, state)
        self.assertEqual(kwargs, dict(org=self.nigeria, osm_id="R23456", name="Lagos", level=1, parent=country_boundary,
                                      geometry=json.dumps(dict(type='MultiPolygon', coordinates=['COORDINATES']))))

        # try creating an object from the kwargs
        Boundary.objects.create(**kwargs)
Beispiel #2
0
    def build_global_boundaries(cls):

        from django.conf import settings
        from temba_client.types import Geometry as TembaGeometry, Boundary as TembaBoundary
        handle = open('%s/geojson/countries.json' % settings.MEDIA_ROOT, 'r+')
        contents = handle.read()
        handle.close()

        boundaries_json = json.loads(contents)

        boundaries = []
        for elt in boundaries_json['features']:
            temba_geometry = TembaGeometry.create(type=elt['geometry']['type'],
                                                  coordinates=elt['geometry']['coordinates'])

            temba_boundary = TembaBoundary.create(level=0, name=elt['properties']['name'],
                                                  boundary=elt['properties']['hc-a2'], geometry=temba_geometry)

            boundaries.append(temba_boundary)

        return boundaries