def create(self, request): # print(request.data) layer = Polygon.objects.get( polygon_id=request.data['layer_id']) polygon = Polygon( polygon_id=request.data['centroid'], centroid=fromstr("POINT(%s %s)" % tuple(request.data['centroid'].split(','))), shape=request.data['shape'], address=request.data['address'], layer=layer, level=Polygon.building, zoom=17, is_verified=True) polygon.save() org_type = OrganizationType.objects.get( type_id=request.data['org_type']) organization = Organization( name=request.data['org_name'], org_type=org_type) organization.save() polygon.organizations.add(organization)
def add_org(request): print(request.POST) layer = Polygon.objects.get( polygon_id=request.POST['layer_id']) polygon = Polygon( polygon_id=request.POST['centroid'], centroid=request.POST['centroid'], address=request.POST['address'], layer=layer, level=Polygon.building, zoom=17, is_verified=True) polygon.save() org_type = OrganizationType.objects.get( type_id=request.POST['org_type']) organization = Organization( name=request.POST['org_name'], org_type=org_type) organization.save() polygon.organizations.add(organization) return HttpResponse(status=201)
def add_org(request): print(request.POST) layer = Polygon.objects.get(polygon_id=request.POST['layer_id']) polygon = Polygon( polygon_id=request.POST['centroid'], centroid=fromstr("POINT(%s %s)" % tuple(request.POST['centroid'].split(','))), shape=request.POST['shape'], address=request.POST['address'], layer=layer, level=Polygon.building, zoom=17, is_verified=True) polygon.save() org_type = OrganizationType.objects.get(type_id=request.POST['org_type']) organization = Organization(name=request.POST['org_name'], org_type=org_type) organization.save() polygon.organizations.add(organization) return HttpResponse(status=201)
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)
def create(self, request): print('yo') parent_polygon_id = request.data.get('parent_polygon_id', None) polygon_id = request.data.get('polygon_id', None) level = request.data.get('level', None) centroid = request.data['centroid'] centroid_pnt = geos.fromstr("POINT(%s %s)" % tuple(centroid.split(','))) layer = None if parent_polygon_id: layer = Polygon.objects.get(polygon_id=parent_polygon_id) else: districts = Polygon.objects.filter(shape__contains=centroid_pnt, level=Polygon.district) if districts: layer = districts[0] if not polygon_id: polygon_id = centroid if not level: level = Polygon.building polygon = Polygon( polygon_id=polygon_id, centroid=centroid_pnt, shape=request.data.get('shape', None), address=request.data['address'], layer=layer, level=level, is_verified=False) polygon.save() org_type = OrganizationType.objects.get( type_id=request.data['org_type']) organization = Organization( name=request.data['name'], org_type=org_type, is_verified=False) organization.save() polygon.organizations.add(organization) serializer = self.get_serializer(organization) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request): print('yo') parent_polygon_id = request.data.get('parent_polygon_id', None) polygon_id = request.data.get('polygon_id', None) level = request.data.get('level', None) centroid = request.data['centroid'] centroid_pnt = geos.fromstr("POINT(%s %s)" % tuple(centroid.split(','))) layer = None if parent_polygon_id: layer = Polygon.objects.get(polygon_id=parent_polygon_id) else: districts = Polygon.objects.filter(shape__contains=centroid_pnt, level=Polygon.district) if districts: layer = districts[0] if not polygon_id: polygon_id = centroid if not level: level = Polygon.building polygon = Polygon(polygon_id=polygon_id, centroid=centroid_pnt, shape=request.data.get('shape', None), address=request.data['address'], layer=layer, level=level, is_verified=False) polygon.save() org_type = OrganizationType.objects.get( type_id=request.data['org_type']) organization = Organization(name=request.data['name'], org_type=org_type, is_verified=False) organization.save() polygon.organizations.add(organization) serializer = self.get_serializer(organization) return Response(serializer.data, status=status.HTTP_201_CREATED)
def geojson_to_db(geo_json): layer_info = geo_json['ctracker_config'] print('Processing %s geojson...' % layer_info['layer_name']) try: layer = Layer.objects.get(name=layer_info['layer_name']) except Layer.DoesNotExist: layer = Layer( layer_type=getattr(Layer, layer_info['layer_type']), name=layer_info['layer_name'], is_default=bool(layer_info['set_default'])) layer.save() for feature in geo_json['features']: # Create polygon 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'], coordinates=json.dumps(feature['geometry']), layer=layer) polygon.save() # Create organization # Temporary, fix unknown organization type org_type = OrganizationType.objects.get(org_type="0") try: org_obj = Organization.objects.get( name=feature['properties']['NAME']) except Organization.DoesNotExist: org_obj = Organization( name=feature['properties']['NAME'], org_type=org_type ) org_obj.save() except Organization.MultipleObjectsReturned: pass # Link them polygon.organizations.add(org_obj) # Temporary, to add additional organizations # to north terminal org_type = OrganizationType.objects.get(org_type="0") north_terminal = Polygon.objects.get(polygon_id='1296') if north_terminal.organizations.all().count() < 3: for new_org in ['Довідкова', 'Каси']: new_org_obj = Organization( name=new_org, org_type=org_type ) new_org_obj.save() north_terminal.organizations.add(new_org_obj)
def create(self, request): layer = Polygon.objects.get(polygon_id=request.data['layer_id']) polygon = Polygon( polygon_id=request.data['centroid'], centroid=geos.fromstr("POINT(%s %s)" % tuple(request.data['centroid'].split(','))), shape=request.data['shape'], address=request.data['address'], layer=layer, level=Polygon.building, zoom=17, is_verified=True) polygon.save() org_type = OrganizationType.objects.get( type_id=request.data['org_type']) organization = Organization(name=request.data['org_name'], org_type=org_type) organization.save() polygon.organizations.add(organization)
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)
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)
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