예제 #1
0
    def geojson_to_db(geo_json, return_instance=False):

        if geo_json['ctracker_config']['AL'] == 4:
            try:
                default_claim_type = ClaimType.objects.get(name='---')
            except ClaimType.DoesNotExist:
                default_claim_type = ClaimType(name='---')
                default_claim_type.save()

        # Create polygons
        for feature in geo_json['features']:
            try:
                polygon = Polygon.objects.get(
                    polygon_id=feature['properties']['ID'])

            except Polygon.DoesNotExist:
                polygon = Polygon(
                    polygon_id=feature['properties']['ID'],
                    shape=json.dumps(feature['geometry']),
                    centroid=feature['properties']['CENTROID'],
                    address=feature['properties']['ADDRESS'],
                    level=geo_json['ctracker_config']['AL'],
                    zoom=geo_json['ctracker_config']['ZOOM'])

                if feature['properties']['PARENT']:
                    parent = Polygon.objects.get(
                        polygon_id=feature['properties']['PARENT'])
                    polygon.layer = parent

                polygon.save()

            if geo_json['ctracker_config']['AL'] == 4:
                org_names = feature['properties']['ORG_NAMES'].split('|')
                org_types = feature['properties']['ORG_TYPES'].split('|')
                for index, org_name in enumerate(org_names):
                    try:
                        org_obj = Organization.objects.get(
                            name=org_name)
                    except Organization.DoesNotExist:

                        try:
                            org_type = OrganizationType.objects.get(
                                type_id=org_types[index])
                        except OrganizationType.DoesNotExist:
                            org_type = OrganizationType(type_id=org_types[index],
                                                        name=geo_json['ctracker_config']["ORG_TYPES"][org_types[index]])
                            org_type.save()
                            default_claim_type.org_type.add(org_type)

                        org_obj = Organization(
                            name=org_name,
                            org_type=org_type
                        )
                        org_obj.save()
                    except Organization.MultipleObjectsReturned:
                        pass

                    # Link them
                    polygon.organizations.add(org_obj)
예제 #2
0
    def handle(self, *args, **options):
        # Organization Types
        print('Creating organization types...')
        for org_type in OrganizationType.ORG_TYPES:
            try:
                OrganizationType.objects.get(org_type=org_type[0])
            except OrganizationType.DoesNotExist:
                obj = OrganizationType(org_type=org_type[0])
                obj.save()
            except OrganizationType.MultipleObjectsReturned:
                pass

        # Polygons n Orgs
        if options['file']:
            geo_json = get_geojson_file(os.path.join(
                settings.INIT_GEOJSON_FOLDER, options['file']))
            GeoJSONParser.geojson_to_db(geo_json)
        else:
            for geo_json_file in os.listdir(settings.INIT_GEOJSON_FOLDER):
                geo_json = get_geojson_file(os.path.join(
                    settings.INIT_GEOJSON_FOLDER, geo_json_file))
                GeoJSONParser.geojson_to_db(geo_json)
예제 #3
0
    def geojson_to_db(geo_json, return_instance=False):
        centroidPattern = re.compile("(-?\d+(?:\.\d+)?)\D+(-?\d+(?:\.\d+)?)")

        if geo_json['ctracker_config']['AL'] == 4:
            try:
                default_claim_type = ClaimType.objects.get(name='---')
            except ClaimType.DoesNotExist:
                default_claim_type = ClaimType(name='---')
                default_claim_type.save()

                with open(os.path.join(settings.INIT_GEOJSON_FOLDER,
                          'habar.jpg'), 'rb') as x_logo:
                    xabar_file = File(x_logo)

                    xabar = ClaimType(name='Xabar', icon=xabar_file)
                    xabar.save()

        # Create polygons
        for feature in geo_json['features']:
            try:
                polygon = Polygon.objects.get(
                    polygon_id=feature['properties']['ID'])

            except Polygon.DoesNotExist:
                polygon = Polygon(
                    polygon_id=feature['properties']['ID'],
                    shape=json.dumps(feature['geometry']),
                    # centroid=GEOSGeometry('POINT(%s %s)') % (
                    #     feature['properties']['CENTROID'].split(',')),
                    centroid=fromstr("POINT(%s %s)" % centroidPattern.search(
                        feature['properties']['CENTROID']).groups(),
                        srid=4326),
                    address=feature['properties']['ADDRESS'],
                    level=geo_json['ctracker_config']['AL'],
                    zoom=geo_json['ctracker_config']['ZOOM'])

                if feature['properties']['PARENT']:
                    parent = Polygon.objects.get(
                        polygon_id=feature['properties']['PARENT'])
                    polygon.layer = parent

                polygon.save()

            if geo_json['ctracker_config']['AL'] == 4:
                org_names = feature['properties']['ORG_NAMES'].split('|')
                org_types = feature['properties']['ORG_TYPES'].split('|')
                for index, org_name in enumerate(org_names):
                    try:
                        org_obj = Organization.objects.get(
                            name=org_name)
                    except Organization.DoesNotExist:

                        try:
                            org_type = OrganizationType.objects.get(
                                type_id=org_types[index])
                        except OrganizationType.DoesNotExist:
                            org_type = OrganizationType(
                                type_id=org_types[index],
                                name=geo_json['ctracker_config']["ORG_TYPES"][org_types[index]])
                            org_type.save()
                            org_type.claimtype_set.add(
                                default_claim_type, xabar)

                        org_obj = Organization(
                            name=org_name,
                            org_type=org_type
                        )
                        org_obj.save()
                    except Organization.MultipleObjectsReturned:
                        pass

                    # Link them
                    polygon.organizations.add(org_obj)
예제 #4
0
    def geojson_to_db(geo_json, return_instance=False):
        centroidPattern = re.compile("(-?\d+(?:\.\d+)?)\D+(-?\d+(?:\.\d+)?)")

        if geo_json['ctracker_config']['AL'] == 4:
            try:
                default_claim_type = ClaimType.objects.get(name='---')
            except ClaimType.DoesNotExist:
                default_claim_type = ClaimType(name='---')
                default_claim_type.save()

            try:
                xabar = ClaimType.objects.get(name='Xabar')
            except ClaimType.DoesNotExist:
                with open(
                        os.path.join(settings.INIT_GEOJSON_FOLDER,
                                     'habar.jpg'), 'rb') as x_logo:
                    xabar_file = File(x_logo)

                    xabar = ClaimType(name='Xabar', icon=xabar_file)
                    xabar.save()

        # Create polygons
        for feature in geo_json['features']:
            try:
                polygon = Polygon.objects.get(
                    polygon_id=feature['properties']['ID'])

            except Polygon.DoesNotExist:
                polygon = Polygon(
                    polygon_id=feature['properties']['ID'],
                    shape=json.dumps(feature['geometry']),
                    # centroid=GEOSGeometry('POINT(%s %s)') % (
                    #     feature['properties']['CENTROID'].split(',')),
                    centroid=fromstr("POINT(%s %s)" % centroidPattern.search(
                        feature['properties']['CENTROID']).groups(),
                                     srid=4326),
                    address=feature['properties']['ADDRESS'],
                    level=geo_json['ctracker_config']['AL'],
                    zoom=geo_json['ctracker_config']['ZOOM'])

                if feature['properties']['PARENT']:
                    parent = Polygon.objects.get(
                        polygon_id=feature['properties']['PARENT'])
                    polygon.layer = parent

                polygon.save()

            if geo_json['ctracker_config']['AL'] == 4:
                org_names = feature['properties']['ORG_NAMES'].split('|')
                org_types = feature['properties']['ORG_TYPES'].split('|')
                for index, org_name in enumerate(org_names):
                    try:
                        org_obj = Organization.objects.get(name=org_name)
                    except Organization.DoesNotExist:

                        try:
                            org_type = OrganizationType.objects.get(
                                type_id=org_types[index])
                        except OrganizationType.DoesNotExist:
                            org_type = OrganizationType(
                                type_id=org_types[index],
                                name=geo_json['ctracker_config']["ORG_TYPES"][
                                    org_types[index]])
                            org_type.save()
                            org_type.claimtype_set.add(default_claim_type,
                                                       xabar)

                        org_obj = Organization(name=org_name,
                                               org_type=org_type)
                        org_obj.save()
                    except Organization.MultipleObjectsReturned:
                        pass

                    # Link them
                    polygon.organizations.add(org_obj)
예제 #5
0
    def geojson_to_db(geo_json, return_instance=False):

        # Create layer
        layer_info = geo_json['ctracker_config']
        global_org_type = False
        if 'global_org_type' in layer_info:
            try:
                global_org_type = OrganizationType.objects.get(
                    type_id=layer_info['global_org_type'])
            except OrganizationType.DoesNotExist:
                global_org_type = OrganizationType(
                    type_id=layer_info['global_org_type'],
                    name=layer_info['global_org_type_name'])
                global_org_type.save()

        print('Processing %s geojson...' % layer_info['layer_name'])
        try:
            layer = Layer.objects.get(name=layer_info['layer_name'])
        except Layer.DoesNotExist:

            if layer_info['set_default']:
                try:
                    ex_default = Layer.objects.get(is_default=True)
                    ex_default.is_default = False
                    ex_default.save()
                except Layer.DoesNotExist:
                    pass

            layer = Layer(
                layer_type=getattr(Layer, layer_info['layer_type']),
                name=layer_info['layer_name'],
                is_default=bool(layer_info['set_default']),
                zoom=layer_info['zoom'],
                center=json.dumps(layer_info['center']))
            layer.save()

        # Create polygons
        for feature in geo_json['features']:
            try:
                polygon = Polygon.objects.get(
                    polygon_id=feature['properties']['ID'])
            except Polygon.DoesNotExist:
                # Hack to avoid organizations without names
                if not feature['properties']['NAME']:
                    continue

                polygon = Polygon(
                    polygon_id=feature['properties']['ID'],
                    shape=json.dumps(feature['geometry']),
                    centroid=json.dumps([
                        feature['properties']["CEN_LAT"],
                        feature['properties']["CEN_LONG"]]),
                    address=feature['properties']['ADDRESS'],
                    layer=layer)
                polygon.save()

            # Create organization
            # Temporary, fix unknown organization type
            org_type = global_org_type

            polygon_orgs = feature['properties']['NAME'].split('|')
            for org_name in polygon_orgs:
                try:
                    org_obj = Organization.objects.get(
                        name=org_name)
                except Organization.DoesNotExist:
                    org_obj = Organization(
                        name=org_name,
                        org_type=org_type
                    )
                    org_obj.save()
                except Organization.MultipleObjectsReturned:
                    pass

                # Link them
                polygon.organizations.add(org_obj)

        if return_instance:
            return layer