def geom_from_boundary_file(boundary_file): """ Opens a local copy of the boundary file and sets geom field Mostly copied from Neighborhood._set_geom_from_boundary_file because we don't have access to model methods here Does not save model Copies the geom of the first feature found in the shapefile into geom, to be consistent with the rest of the app No explicit error handling/logging, will raise original exception if failure """ geom = None try: tmpdir = tempfile.mkdtemp() local_zipfile = os.path.join(tmpdir, 'neighborhood.zip') with open(local_zipfile, 'wb') as zip_handle: zip_handle.write(boundary_file.read()) with zipfile.ZipFile(local_zipfile, 'r') as zip_handle: zip_handle.extractall(tmpdir) shpfiles = [ filename for filename in os.listdir(tmpdir) if filename.endswith('shp') ] shp_filename = os.path.join(tmpdir, shpfiles[0]) with fiona.open(shp_filename, 'r') as shp_handle: feature = next(shp_handle) geom = GEOSGeometry(json.dumps(feature['geometry'])) if geom.geom_type == 'Polygon': geom = MultiPolygon([geom]) except: geom = None logger.exception('ERROR: {}'.format(str(boundary_file))) finally: shutil.rmtree(tmpdir, ignore_errors=True) return geom
def test_can_create_mapelement(self): ds = Dataset.objects.get(pk=1) p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1))) mpoly = MultiPolygon(p1, p2) polygon = MapPolygon(lat='42.7302', lon='73.6788', field1=1.0, field2=2.0, mpoly=mpoly, dataset=ds) polygon.save() mp = MapPoint.objects.get(pk=1) original_count = MapElement.objects.all().count() me = MapElement(dataset=ds, remote_id='123', name='element', mappolygon=polygon, mappoint=mp) me.save() self.assertEqual(MapElement.objects.all().count(), original_count + 1) self.assertEqual(me.name, 'element') self.assertEqual(me.polygon_id(), me.id) self.assertEqual(me.point_id(), me.id)
def chop_geom(multipolygon, fraction): """ Transforms each point fraction the distance to the geometry's centroid to form a smaller geometry :param geom: :return: a multipolygon reduced by the fraction from the original """ def transform_polygon(polygon): def transform_linear_ring(linear_ring): centroid = polygon.centroid return LinearRing( map( lambda point: to_tuple( LineString((point, centroid)).interpolate( fraction, normalized=True)), linear_ring)) linear_rings = map( lambda linear_ring: transform_linear_ring(linear_ring), polygon) if len(linear_rings) > 1: return Polygon(linear_rings[0], [linear_rings[1:]]) else: return Polygon(linear_rings[0], []) return MultiPolygon( map(lambda polygon: transform_polygon(polygon), multipolygon))
def label_challenge_edit(request, unique_id): challenge = get_object_or_404(LabelChallenge, unique_id=unique_id) if request.method == "POST": form = LabelChallengeForm(request.POST, instance=challenge) new_label_types = request.POST.get('new_label_types', '') print(new_label_types) if form.is_valid(): challenge = form.save(commit=False) challenge.user = request.user challenge.updated_at = datetime.now() challenge.save() geometry = json.loads(challenge.geometry) multipolygon_ary = [] for geo in geometry: coordinates = geo['geometry']['coordinates'] print(coordinates) multipolygon_ary.append(Polygon(coordinates[0])) challenge.multipolygon = MultiPolygon(multipolygon_ary) for geo in geometry: geo['properties']['challenge_id'] = str(challenge.unique_id) challenge.geometry = json.dumps(geometry) challenge.save() label_type = form.cleaned_data['label_type'] if not label_type is None: challenge.label_type.clear() for label_t in label_type: challenge.label_type.add(label_t) if not new_label_types is None and new_label_types != '': label_types = new_label_types.split(',') if len(label_types) > 0: for label_type in label_types: l_ary = label_type.split('--') index = 0 if len(l_ary) > 0: l_type = None for l in l_ary: if index == 0: types = LabelType.objects.filter( parent__isnull=True, name=l) else: types = LabelType.objects.filter( parent=l_type, name=l) if types.count() == 0: l_parent_type = l_type l_type = LabelType() l_type.name = l l_type.parent = l_parent_type print(l_type.name) l_type.save() else: l_type = types[0] index += 1 if index == len(l_ary): challenge.label_type.add(l_type) messages.success( request, 'Challenge "%s" is updated successfully.' % challenge.name) return redirect('challenge.label_challenge_list') else: form = LabelChallengeForm(instance=challenge) label_types = LabelType.objects.filter(parent__isnull=True, source='mtpw') content = { 'form': form, 'pageName': 'Edit Label Challenge', 'challenge': challenge, 'pageTitle': challenge.name + ' - Edit Label Challenge', 'label_types': label_types } return render(request, 'challenge/label/edit.html', content)
def challenge_edit(request, unique_id): challenge = get_object_or_404(Challenge, unique_id=unique_id) if request.method == "POST": form = ChallengeForm(request.POST, instance=challenge) if form.is_valid(): challenge = form.save(commit=False) challenge.user = request.user challenge.updated_at = datetime.now() challenge.save() camera_make = form.cleaned_data['camera_make'] print(camera_make) if not camera_make is None and len(camera_make) > 0: challenge.transport_type.clear() for cm in camera_make: challenge.camera_make.add(cm) geometry = json.loads(challenge.geometry) multipolygon = MultiPolygon() for geo in geometry: coordinates = geo['geometry']['coordinates'][0] line_string = LineString() first_point = None for i in range(len(coordinates)): coord = coordinates[i] if i == 0: first_point = Point(coord[0], coord[1]) continue point = Point(coord[0], coord[1]) if i == 1: line_string = LineString(first_point.coords, point.coords) else: line_string.append(point.coords) polygon = Polygon(line_string.coords) multipolygon.append(polygon) challenge.multipolygon = multipolygon for geo in geometry: geo['properties']['challenge_id'] = str(challenge.unique_id) challenge.geometry = json.dumps(geometry) challenge.save() transport_type = form.cleaned_data['transport_type'] print(transport_type.count()) if transport_type is not None: challenge.transport_type.clear() for transport_t in transport_type: challenge.transport_type.add(transport_t) messages.success( request, 'Challenge "%s" is updated successfully.' % challenge.name) return redirect('challenge.index') else: form = ChallengeForm(instance=challenge) content = { 'form': form, 'pageName': 'Edit Challenge', 'challenge': challenge, 'pageTitle': challenge.name + ' - Edit Challenge' } return render(request, 'challenge/capture/edit.html', content)
def handle(self, *args, **options): filename = options['filename'][0] import_missing = [] if options['import_missing']: import_file = open(options['import_missing'], 'r') import_missing = import_file.read().splitlines() print('will import these isos if found', import_missing) else: print('will write missing country iso to missing-countries.txt') missing_file = open('missing-countries.txt', 'w') duplicate_file = open('duplicate-countries.txt', 'w') country_centroids = {} if options['update_centroid']: centroid_file = csv.DictReader( open(options['update_centroid'], 'r'), fieldnames=['country', 'latitude', 'longitude', 'name']) next(centroid_file) for row in centroid_file: code = row['country'] lat = row['latitude'] lon = row['longitude'] if (lat != '' and lon != ''): country_centroids[code] = Point(float(lon), float(lat)) iso3_codes = {} if options['update_iso3']: iso3_file = csv.DictReader(open(options['update_iso3'], 'r'), fieldnames=['iso2', 'iso3']) next(iso3_file) for row in iso3_file: iso3_codes[row['iso2']] = row['iso3'] try: data = DataSource(filename) except: raise CommandError('Could not open file') fields = data[0].fields # first, let's import all the geometries for countries with iso code for feature in data[0]: feature_iso2 = feature.get('ISO2') geom_wkt = feature.geom.wkt geom = GEOSGeometry(geom_wkt, srid=4326) if (geom.geom_type == 'Polygon'): geom = MultiPolygon(geom) centroid = geom.centroid.wkt bbox = geom.envelope.geojson if feature_iso2: # find this country in the database try: # if the country exist country = Country.objects.get(iso=feature_iso2, record_type=1) if options['update_geom']: # check if this geom exists try: CountryGeom = CountryGeoms.objects.get( country=country) CountryGeom.geom = geom.wkt CountryGeom.save() except ObjectDoesNotExist: # add geom CountryGeom = CountryGeoms() CountryGeom.country = country CountryGeom.geom = geom.wkt CountryGeom.save() if options['update_bbox']: # add bbox country.bbox = bbox if options['update_centroid']: # add centroid if (feature_iso2 in country_centroids.keys()): country.centroid = country_centroids[feature_iso2] else: country.centroid = centroid if options['update_iso3']: if (feature_iso2 in iso3_codes.keys()): print('updating iso3', iso3_codes[feature_iso2]) country.iso3 = iso3_codes[feature_iso2] if options['update_independent']: if ('INDEPENDEN' in fields): independent = feature.get('INDEPENDEN') if independent == 'TRUE': country.independent = True elif independent == 'FALSE': country.independent = False else: country.independent = None # save if options['update_geom'] or options[ 'update_bbox'] or options[ 'update_centroid'] or options[ 'update_iso3'] or options[ 'update_independent']: print('updating %s' % feature_iso2) country.save() except MultipleObjectsReturned: if not options['import_missing']: name = feature.get('NAME_ICRC') duplicate_file.write(feature_iso2 + '\n') print('duplicate country', feature_iso2, name) except ObjectDoesNotExist: if not options['import_missing']: name = feature.get('NAME_ICRC') missing_file.write(feature_iso2 + '\n') print('missing country', feature_iso2, name) else: # check if this iso exists in the import list if feature_iso2 in import_missing: print('importing', feature_iso2) self.add_country(feature, geom, centroid, bbox, feature_iso2, fields) else: print('skipping', feature_iso2) else: if (options['import_all']): print( 'Geometry with no iso code found, but will still import.' ) self.add_country(feature, geom, centroid, bbox, None, fields) print('done!')
def watershed_shapefile(request, instances): from arp.models import PlanningUnit, WatershedPrioritization, PlanningUnitShapes wshds = PlanningUnit.objects.all() stamp = int(time.time() * 1000.0) for instance in instances: viewable, response = instance.is_viewable(request.user) if not viewable: return response if not isinstance(instance, WatershedPrioritization): return HttpResponse( "Shapefile export for watershed prioritizations only", status=500) ob = json.loads(instance.output_best) wids = [int(x.strip()) for x in ob['best']] puc = json.loads(instance.output_pu_count) for ws in wshds: # create custom model records pus, created = PlanningUnitShapes.objects.get_or_create( pu=ws, stamp=stamp) # Only do this on the first go 'round if created and not pus.geometry: pus.name = ws.name pus.fid = ws.fid p = ws.geometry.simplify(100) if p.geom_type == 'Polygon': pus.geometry = MultiPolygon(p) elif p.geom_type == 'MultiPolygon': pus.geometry = p # Calculate hits and best try: hits = puc[str(ws.pk)] except: hits = 0 best = ws.pk in wids pus.hits += hits if best: pus.bests += 1 pus.save() readme = """Watershed Prioritization Analysis contact: [email protected] Includes scenarios: %s Contains HUC8s from Oregon, Washington, Idaho 'bests' contains the number of scenarios in which the subbasin was included in the best run 'hits' contains the number of times the subbasin was included in any run, cumulative across scenarios. """ % ('\n '.join([i.name for i in instances]), ) allpus = PlanningUnitShapes.objects.filter(stamp=stamp) shp_response = ShpResponder(allpus, readme=readme) filename = '_'.join([slugify(i.pk) for i in instances]) shp_response.file_name = slugify('wp_' + filename) return shp_response()
def setUp(self): ###### # Request/Render mock ###### def local_render_to_response(*args, **kwargs): from django.template import loader, RequestContext from django.http import HttpResponse httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} hr = HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response ###### # Content types ###### r1 = ReputationAction(name="edit verified", description="blah") r2 = ReputationAction(name="edit tree", description="blah") r3 = ReputationAction(name="Administrative Action", description="blah") r4 = ReputationAction(name="add tree", description="blah") r5 = ReputationAction(name="edit plot", description="blah") r6 = ReputationAction(name="add plot", description="blah") r7 = ReputationAction(name="add stewardship", description="blah") r8 = ReputationAction(name="remove stewardship", description="blah") self.ra = [r1, r2, r3, r4, r5, r6, r7, r8] for r in self.ra: r.save() ###### # Set up benefit values ###### bv = BenefitValues(co2=0.02, pm10=9.41, area="InlandValleys", electricity=0.1166, voc=4.69, ozone=5.0032, natural_gas=1.25278, nox=12.79, stormwater=0.0078, sox=3.72, bvoc=4.96) bv.save() self.bv = bv dbh = "[1.0, 2.0, 3.0]" rsrc = Resource(meta_species="BDM_OTHER", electricity_dbh=dbh, co2_avoided_dbh=dbh, aq_pm10_dep_dbh=dbh, region="Sim City", aq_voc_avoided_dbh=dbh, aq_pm10_avoided_dbh=dbh, aq_ozone_dep_dbh=dbh, aq_nox_avoided_dbh=dbh, co2_storage_dbh=dbh, aq_sox_avoided_dbh=dbh, aq_sox_dep_dbh=dbh, bvoc_dbh=dbh, co2_sequestered_dbh=dbh, aq_nox_dep_dbh=dbh, hydro_interception_dbh=dbh, natural_gas_dbh=dbh) rsrc.save() self.rsrc = rsrc ###### # Users ###### u = User.objects.filter(username="******") if u: u = u[0] else: u = User.objects.create_user("jim", "*****@*****.**", "jim") u.is_staff = True u.is_superuser = True u.save() up = UserProfile(user=u) u.reputation = Reputation(user=u) u.reputation.save() self.u = u ####### # Setup geometries -> Two stacked 100x100 squares ####### n1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) n2geom = MultiPolygon( Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101)))) n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom) n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom) n1.save() n2.save() z1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) z2geom = MultiPolygon( Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100)))) z1 = ZipCode(zip="19107", geometry=z1geom) z2 = ZipCode(zip="10001", geometry=z2geom) z1.save() z2.save() exgeom1 = MultiPolygon( Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0)))) ex1 = ExclusionMask(geometry=exgeom1, type="building") ex1.save() agn1 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n1) agn2 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n2) agn1.save() agn2.save() self.agn1 = agn1 self.agn2 = agn2 self.z1 = z1 self.z2 = z2 self.n1 = n1 self.n2 = n2 ###### # And we could use a few species... ###### s1 = Species(symbol="s1", genus="testus1", species="specieius1") s2 = Species(symbol="s2", genus="testus2", species="specieius2") s1.save() s2.save() self.s1 = s1 self.s2 = s2 ####### # Create some basic plots ####### ie = ImportEvent(file_name='site_add') ie.save() self.ie = ie p1_no_tree = Plot(geometry=Point(50, 50), last_updated_by=u, import_event=ie, present=True, data_owner=u) p1_no_tree.save() p2_tree = Plot(geometry=Point(51, 51), last_updated_by=u, import_event=ie, present=True, data_owner=u) p2_tree.save() p3_tree_species1 = Plot(geometry=Point(50, 100), last_updated_by=u, import_event=ie, present=True, data_owner=u) p3_tree_species1.save() p4_tree_species2 = Plot(geometry=Point(50, 150), last_updated_by=u, import_event=ie, present=True, data_owner=u) p4_tree_species2.save() t1 = Tree(plot=p2_tree, species=None, last_updated_by=u, import_event=ie) t1.present = True t1.save() t2 = Tree(plot=p3_tree_species1, species=s1, last_updated_by=u, import_event=ie) t2.present = True t2.save() t3 = Tree(plot=p4_tree_species2, species=s2, last_updated_by=u, import_event=ie) t3.present = True t3.save() self.p1_no_tree = p1_no_tree self.p2_tree = p2_tree self.p3_tree_species1 = p3_tree_species1 self.p4_tree_species2 = p4_tree_species2 self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2] self.t1 = t1 self.t2 = t2 self.t3 = t3
def generate_multi_polygon(): return MultiPolygon(generate_polygon())
def convert_polygon(geom): if geom and isinstance(geom, Polygon): return MultiPolygon(geom, srid=geom.srid)
def multi_polygon_from(bounding_geometry_old): bb = bounding_geometry_old west, south, east, north = bb.south_west[0], bb.south_west[ 1], bb.north_east[0], bb.north_east[1] polygon = Polygon.from_bbox((west, south, east, north)) return MultiPolygon(polygon)
def footprint(self): return MultiPolygon(*self.polygons)
def create_from_box(cls, x_min, y_min, x_max, y_max): """Create from box (Web Mercator coordinates)""" bounds = Polygon(((x_min, y_min), (x_min, y_max), (x_max, y_max), (x_max, y_min), (x_min, y_min))) bounds = MultiPolygon((bounds, )) return InstanceBounds.objects.create(geom=bounds)
def save_related(self, request, form, formsets, change): super(ClosureNoticeMaineAdmin, self).save_related(request, form, formsets, change) notice_obj = form.instance if notice_obj.border_east.exists() or notice_obj.border_west.exists(): try: base_shape = BaseAreaShape.objects.get(name='Maine Coastline') except BaseAreaShape.DoesNotExist: base_shape = None if base_shape: base_polygon = base_shape.geom # Set up the default ME borders in case only one border is different default_borders = {} default_borders['area-100-a-east'] = [(-69.488525, 43.927572), (-69.513031, 43.831688), (-69.509811, 43.654460)] default_borders['area-100-a-west'] = [(-71.262817, 43.393074), (-70.540466, 42.890051), (-70.543213, 42.809507)] default_borders['area-100-b-east'] = [(-68.846420, 44.458964), (-68.863179, 44.258705), (-68.868086, 44.214246), (-68.783640, 44.189524), (-68.719059, 44.166838), (-68.727250, 44.014249), (-68.431091, 43.721490)] default_borders['area-100-b-west'] = [(-69.488525, 43.927572), (-69.513031, 43.831688), (-69.509811, 43.654460)] default_borders['area-64-a-east'] = [(-67.395363, 44.69667), (-67.339325, 44.639346), (-67.317352, 44.370987)] default_borders['area-64-a-west'] = [(-68.877531, 44.443131), (-68.88024, 44.39334), (-68.931997, 44.2335), (-68.846142, 44.18396879), (-68.782654, 43.946361)] default_borders['area-64-b-east'] = [(-67.066040, 45.650528), (-66.331398, 43.927572), (-65.494995, 44.020472)] default_borders['area-64-b-west'] = [(-67.395363, 44.69667), (-67.339325, 44.639346), (-67.317352, 44.370987)] default_borders['area-164-a-east'] = [(-68.075428, 44.666838), (-68.060993, 44.333023), (-68.027344, 43.592328)] default_borders['area-164-a-west'] = [(-71.262817, 43.393074), (-70.540466, 42.890051), (-70.543213, 42.809507)] default_borders['area-164-b-east'] = [(-67.066040, 45.650528), (-66.192627, 44.972571), (-65.494995, 44.020472)] default_borders['area-164-b-west'] = [(-68.075428, 44.666838), (-68.060993, 44.333023), (-68.027344, 43.592328)] # Set the distance to go North/South from final points to set polygon mask distance_togo_north = geopy.distance.distance(kilometers=80) distance_togo_south = geopy.distance.distance(kilometers=100) if not notice_obj.border_east.exists(): # If no custom border_east, set it to the default border for this Shellfish Area shellfish_area = notice_obj.shellfish_areas.first() border_east_name = slugify(shellfish_area.name) + '-east' border_east_coords = default_borders[border_east_name] else: # Create custom eastern border border_east_coords = [] border_east = notice_obj.border_east.all().annotate( lat=ExpressionWrapper( Func('geom', function='ST_Y'), output_field=FloatField())).order_by('-lat') for i, point in enumerate(border_east): # Need to check if this is the Canadian border if point.name == 'Maine/Canada Border': border_east_coords = default_borders[ 'area-164-b-east'] break else: # most northern point if i == 0: # Create new Point that is some distance North of last point north_point = distance_togo_north.destination( point=(point.geom.y, point.geom.x), bearing=355) # convert geopy point to GEOS north_point_geos = Point( north_point.longitude, north_point.latitude) border_east_coords.append( north_point_geos.coords) # add the selected point to line border_east_coords.append(point.geom.coords) # most southern point if i == len(border_east) - 1: # Create new Point that is 100km South of last point south_point = distance_togo_south.destination( point=(point.geom.y, point.geom.x), bearing=180) # convert geopy point to GEOS south_point_geos = Point( south_point.longitude, south_point.latitude) border_east_coords.append( south_point_geos.coords) if not notice_obj.border_west.exists(): # If no custom border_west, set it to the default border for this Shellfish Area shellfish_area = notice_obj.shellfish_areas.first() border_west_name = slugify(shellfish_area.name) + '-west' border_west_coords = default_borders[border_west_name] else: # Create custom western border border_west_coords = [] border_west = notice_obj.border_west.all().annotate( lat=ExpressionWrapper( Func('geom', function='ST_Y'), output_field=FloatField())).order_by('-lat') for i, point in enumerate(border_west): # Need to check if this is the NH border if point.name == 'Maine/NH Border': border_west_coords = default_borders[ 'area-100-a-west'] break else: # most northern point if i == 0: north_point = distance_togo_north.destination( point=(point.geom.y, point.geom.x), bearing=355) # convert geopy point to GEOS north_point_geos = Point( north_point.longitude, north_point.latitude) border_west_coords.append( north_point_geos.coords) # add the selected point to line border_west_coords.append(point.geom.coords) # most southern point if i == len(border_west) - 1: south_point = distance_togo_south.destination( point=(point.geom.y, point.geom.x), bearing=180) # convert geopy point to GEOS south_point_geos = Point( south_point.longitude, south_point.latitude) border_west_coords.append( south_point_geos.coords) # Create polygon from custom border lines # reverse one of the border lines to make the point list a valid polygon border_east_coords.reverse() # make one list of coordinates polygon_line = border_west_coords + border_east_coords # add the first point to the end to create linear ring polygon_line.append(polygon_line[0]) # create polygon polygon_mask = Polygon(polygon_line) """ border_east_linestring = LineString([coords for coords in border_east_coords]) border_west_linestring = LineString([coords for coords in border_west_coords]) multi_line = MultiLineString(border_east_linestring, border_west_linestring) # create polygon from custom border lines polygon_mask = multi_line.convex_hull # create new geometry from base map with the mask """ new_shape = base_polygon.intersection(polygon_mask) if isinstance(new_shape, Polygon): new_shape = MultiPolygon(new_shape) notice_obj.custom_geom = new_shape notice_obj.save()
def import_shape(self, store, config, updatefield): """ Import a shapefile, based on a config. Parameters: config -- A dictionary with 'shapepath', 'geolevel', 'name_field', 'region_filters' and 'subject_fields' keys. """ def get_shape_tree(shapefile, feature): shpfields = shapefile.xpath('Fields/Field') builtid = '' for idx in range(0, len(shpfields)): idpart = shapefile.xpath( 'Fields/Field[@type="tree" and @pos=%d]' % idx) if len(idpart) > 0: idpart = idpart[0] part = feature.get(idpart.get('name')) # strip any spaces in the treecode if not (isinstance(part, types.StringTypes)): part = '%d' % part part = part.strip(' ') width = int(idpart.get('width')) builtid = '%s%s' % (builtid, part.zfill(width)) return builtid def get_shape_portable(shapefile, feature): field = shapefile.xpath('Fields/Field[@type="portable"]')[0] portable = feature.get(field.get('name')) if not (isinstance(portable, types.StringTypes)): portable = '%d' % portable return portable def get_shape_name(shapefile, feature): field = shapefile.xpath('Fields/Field[@type="name"]')[0] strname = feature.get(field.get('name')) if type(strname) == str: return strname.decode('latin-1') else: return str(strname) for h, shapefile in enumerate(config['shapefiles']): if not exists(shapefile.get('path')): logger.info( """ ERROR: The filename specified by the configuration: %s Could not be found. Please check the configuration and try again. """, shapefile.get('path')) raise IOError('Cannot find the file "%s"' % shapefile.get('path')) ds = DataSource(shapefile.get('path')) logger.info('Importing from %s, %d of %d shapefiles...', ds, h + 1, len(config['shapefiles'])) lyr = ds[0] logger.info('%d objects in shapefile', len(lyr)) level = Geolevel.objects.get(name=config['geolevel'].lower()[:50]) # Create the subjects we need subject_objects = {} for sconfig in config['subject_fields']: attr_name = sconfig.get('field') foundalias = False for elem in sconfig.getchildren(): if elem.tag == 'Subject': foundalias = True sub = Subject.objects.get( name=elem.get('id').lower()[:50]) if not foundalias: sub = Subject.objects.get( name=sconfig.get('id').lower()[:50]) subject_objects[attr_name] = sub subject_objects['%s_by_id' % sub.name] = attr_name progress = 0.0 logger.info('0% .. ') for i, feat in enumerate(lyr): if (float(i) / len(lyr)) > (progress + 0.1): progress += 0.1 logger.info('%2.0f%% .. ', progress * 100) levels = [level] for region, filter_list in config['region_filters'].iteritems( ): # Check for applicability of the function by examining the config geolevel_xpath = '/DistrictBuilder/GeoLevels/GeoLevel[@name="%s"]' % config[ 'geolevel'] geolevel_config = store.data.xpath(geolevel_xpath) geolevel_region_xpath = '/DistrictBuilder/Regions/Region[@name="%s"]/GeoLevels//GeoLevel[@ref="%s"]' % ( region, geolevel_config[0].get('id')) if len(store.data.xpath(geolevel_region_xpath)) > 0: # If the geolevel is in the region, check the filters for f in filter_list: if f(feat) is True: levels.append( Geolevel.objects.get(name='%s_%s' % (region, level.name))) shape_name = get_shape_name(shapefile, feat) shape_portable_id = get_shape_portable(shapefile, feat) shape_tree_code = get_shape_tree(shapefile, feat) prefetch = Geounit.objects.filter( name=shape_name, geolevel__in=levels, portable_id=shape_portable_id, tree_code=shape_tree_code) should_create = prefetch.count() == 0 if should_create: try: # Store the geos geometry # Buffer by 0 to get rid of any self-intersections which may make this geometry invalid. geos = feat.geom.geos.buffer(0) # Coerce the geometry into a MultiPolygon if geos.geom_type == 'MultiPolygon': my_geom = geos elif geos.geom_type == 'Polygon': my_geom = MultiPolygon(geos) simple = my_geom.simplify(tolerance=Decimal( config['tolerance']), preserve_topology=True) if simple.geom_type != 'MultiPolygon': simple = MultiPolygon(simple) center = my_geom.centroid geos = None # Ensure the centroid is within the geometry if not center.within(my_geom): # Get the first polygon in the multipolygon first_poly = my_geom[0] # Get the extent of the first poly first_poly_extent = first_poly.extent min_x = first_poly_extent[0] max_x = first_poly_extent[2] # Create a line through the bbox and the poly center my_y = first_poly.centroid.y centerline = LineString((min_x, my_y), (max_x, my_y)) # Get the intersection of that line and the poly intersection = centerline.intersection(first_poly) if type(intersection) is MultiLineString: intersection = intersection[0] # the center of that line is my within-the-poly centroid. center = intersection.centroid first_poly = first_poly_extent = min_x = max_x = my_y = centerline = intersection = None g = Geounit(geom=my_geom, name=shape_name, simple=simple, center=center, portable_id=shape_portable_id, tree_code=shape_tree_code) g.save() g.geolevel = levels g.save() except: logger.info('Failed to import geometry for feature %d', feat.fid) logger.info(traceback.format_exc()) continue else: g = prefetch[0] g.geolevel = levels g.save() if not config['attributes']: # If we created a new Geounit, we can let this function know that it doesn't # need to check for existing Characteristics, which will speed things up # significantly. self.set_geounit_characteristic(g, subject_objects, feat, not should_create, updatefield) logger.info('100%') if config['attributes']: progress = 0 logger.info("Assigning subject values to imported geography...") logger.info('0% .. ') for h, attrconfig in enumerate(config['attributes']): if not exists(attrconfig.get('path')): logger.info( """ ERROR: The filename specified by the configuration: %s Could not be found. Please check the configuration and try again. """, attrconfig.get('path')) raise IOError('Cannot find the file "%s"' % attrconfig.get('path')) lyr = DataSource(attrconfig.get('path'))[0] for i, feat in enumerate(lyr): if (float(i) / len(lyr)) > (progress + 0.1): progress += 0.1 logger.info('%2.0f%% .. ', progress * 100) gid = get_shape_tree(attrconfig, feat) g = Geounit.objects.filter(tree_code=gid) if g.count() > 0: self.set_geounit_characteristic( g[0], subject_objects, feat, True, updatefield) logger.info('100%')
def load_multipolygon(cls, rd_coordinates): polygons = [] for entry in rd_coordinates: polygons.append(cls.load_polygon(entry)) return MultiPolygon(polygons, srid=28992)
def handle(self, *args, **options): Country.objects.all().delete() data_source = DataSource( 'localities/data/ne_10m_admin_0_countries.shp') layer = data_source[0] for feature in layer: country_name = feature['NAME'].value country_name = country_name.encode('utf-8') country_name_output = country_name # ------------------------------------------------- # CORRECTING THE NAME # ------------------------------------------------- country_name = country_name.replace(" (Petrel Is.)", "") country_name = country_name.replace("Barb.", "Barbuda") country_name = country_name.replace("Br.", "British") country_name = country_name.replace("Central African Rep.", "Central African Republic") country_name = country_name.replace("Curaçao", "Curacao") country_name = country_name.replace("Côte d'Ivoire", "Ivory Coast") country_name = country_name.replace( "Cyprus U.N. Buffer Zone", "United Nations Buffer Zone in Cyprus") country_name = country_name.replace( "Dem. Rep. Congo", "Democratic Republic of the Congo") country_name = country_name.replace("Dem. Rep. Korea", "North Korea") country_name = country_name.replace("Eq.", "Equatorial") country_name = country_name.replace( "Fr. S. Antarctic Lands", "French Southern and Antarctic Lands") country_name = country_name.replace("Fr.", "French") country_name = country_name.replace("Geo.", "Georgia") country_name = country_name.replace("Gren.", "the Grenadines") country_name = country_name.replace("Herz.", "Herzegovina") country_name = country_name.replace("I.", "Islands") country_name = country_name.replace("Is.", "Islands") country_name = country_name.replace("N.", "Northern") country_name = country_name.replace("Rep.", "Republic") country_name = country_name.replace("U.S.", "United States") country_name = country_name.replace("S.", "South") country_name = country_name.replace("Sandw.", "Sandwich") country_name = country_name.replace("São Tomé and Principe", "Sao Tome and Principe") country_name = country_name.replace("St-Barthélemy", "Saint Barthelemy") country_name = country_name.replace("St.", "Saint") country_name = country_name.replace("St-", "Saint ") country_name = country_name.replace("Ter.", "Territory") country_name = country_name.replace("Vin.", "Vincent") country_name = country_name.replace("W.", "Western") country_name = country_name.strip() # ------------------------------------------------- # FINISH # ------------------------------------------------- geometry = feature.geom try: country = Country.objects.get(name=country_name) if 'MultiPolygon' not in geometry.geojson: geometry = MultiPolygon([ Polygon(coords) for coords in country.geometry.coords[0] ] + [Polygon(geometry.coords[0])]).geojson else: geometry = MultiPolygon([ Polygon(coords) for coords in country.geometry.coords[0] ] + [Polygon(coords) for coords in geometry.coords[0]]).geojson country.polygon_geometry = geometry except: if 'MultiPolygon' not in geometry.geojson: geometry = MultiPolygon(Polygon( geometry.coords[0])).geojson else: geometry = geometry.geojson country = Country(name=country_name) country.polygon_geometry = geometry if not country_name in self.non_countries: country.save()
class CityFactory(factory.Factory): FACTORY_FOR = models.City code = factory.Sequence(lambda n: u"#%s" % n) # id (!) with max_length=6 name = factory.Sequence(lambda n: u"City name %s" % n) geom = factory.Sequence(lambda _: MultiPolygon(Polygon.from_bbox(geom_city_iter.next()), srid=settings.SRID))
def run(path): print "starting load.run with ", path admin_level = None country_code = None path_to_tmp = "/tmp" #clear the tmp directory call(["rm","-fr","/tmp/*"]) paths_to_shps = [] if path.startswith("http"): content_type = head(path).headers['content-type'] print "content_type = ", content_type if content_type == "application/zip": print "content-type is application/zip" if "geoserver/wfs" in path: dirname = unquote(search("(?<=typename=)[^&]*",path).group(0)).replace(":","_") print "dirname is", dirname extension = "zip" path_to_directory = path_to_tmp + "/" + dirname print "path_to_directory is", path_to_directory if not isdir(path_to_directory): mkdir(path_to_directory) download(path, path_to_directory) elif path.endswith(".zip"): print "zip" filename = unquote(path.split("?")[0].split("/")[-1]).split(".")[0] path_to_directory = path_to_tmp + "/" + filename if not isdir(path_to_directory): mkdir(path_to_directory) download(path, path_to_directory) elif content_type.startswith("text/html"): print "content-type is text/html" dirname = path.replace(".","_").replace("/","_").replace("-","_").replace(":","_").replace("___","_").replace("__","_") print "dirname is", dirname path_to_directory = path_to_tmp + "/" + dirname print "path_to_directory is", path_to_directory if not isdir(path_to_directory): mkdir(path_to_directory) print "made directory: ", path_to_directory text = get(path).text print "text is", type(text), len(text) soup = BeautifulSoup(text, "lxml") print "soup is", type(soup) if soup.title: title = soup.title.text.lower() admin_level = get_admin_level_from_string(title) # can be set to None if none found downloadables = soup.select("a[href$=zip]") print "number_of_downloadables = ", number_of_downloadables hrefs = [a['href'] for a in soup.select("a[href$=zip]")] for href in hrefs: download(href, path_to_directory) paths_to_gis_files = find_gis_files_in_directory(path_to_directory) print "path_to_gis_files = ", paths_to_gis_files """ if path.startswith("http"): print "passed in a urle( url = path #filename, extension = ".".join(url.split(".")[-2:]).split("/")[-1] filename, extension = url.split("/")[-1].split(".")[-2:] print "filename is", filename print "extension is", extension path_to_dir = "/tmp/" + filename if not isdir(path_to_dir): mkdir(path_to_dir) filepath = path_to_dir + "/" + filename + "." + extension if not isfile(filepath): urlretrieve(url, filepath) else: filepath = path if filepath.endswith("zip"): print filepath, "ends wi zip" with zipfile.ZipFile(filepath, "r") as z: z.extractall('/tmp/') filepath_no_ext = "".join(filepath.split(".")[:-1]) path_to_gis_file = filepath_no_ext + ".shp" print "path_to_gis_file = ", path_to_gis_file paths_to_shp = [path_to_gis_file] """ for path_to_gis_file in paths_to_gis_files: print "for path_to_gis_file", path_to_gis_file # can be None if none found admin_level = get_admin_level_from_string(path_to_gis_file) print "admin_level is", admin_level ds = DataSource(path_to_gis_file) print "ds is", ds print "dir(ds) = ", dir(ds) layers = list(ds) print "layers are", layers layer_parsing = [(layer, parse_layer(layer)) for layer in layers] # we want to sort the layers by admin level # we add the top-level parents in first, # so we can link a new feature to its existing parent layer_parsing = sorted(layer_parsing, key = lambda tup: tup[1]['admin_level']) for layer, d in layer_parsing: print "layer is", layer #raw_input() print "dir(layer) is", dir(layer) print "layer.fields = ", layer.fields if "name" not in d or not d['name']: print "couldn't get name layer, so skip over this layer" continue else: print "successfully got name", d['name'] #raw_input() if 'admin_level' in d and d['admin_level']: admin_level = d['admin_level'] geom_type = str(layer.geom_type) features = list(layer) print "we shuffle the features because later on if we hit the same country_code 20 times, we don't try to get it by db lookup anymore" shuffle(features) #number_of_features = len(features) print "\tnumber_of_features = ", number_of_features == len(features) #raw_input() country_code = None country_codes = set() places_added = [] for i, feature in enumerate(features): try: # after we have gone through 10 features # which were randomly shufffled # if same country_code for all of them # then set country_code # and don't make db call to find out cc again if i == 10: print "\n\nnumber_of_country_codes = ", number_of_country_codes print "CHECK number_of_country_codes = ", number_of_country_codes == len(country_codes) if number_of_country_codes == 1: cc = country_codes.pop() if cc is not None: country_code = cc # if i % 500 != 0: continue print "\nfor feature", i, "of", number_of_features fields = {'admin_level': admin_level} print "d['name'] = ", d['name'] name = feature.get(d['name']) print "name is", name if len(name) > 2: fields['name'] = name else: continue #print "admin_level is", admin_level, "." #if "admin_level" in d: # admin_level = feature.get(d['admin_level']) # if isinstance(admin_level, str) or isinstance(admin_level, unicode): # if admin_level.isdigit(): # admin_level = int(admin_level) # elif admin_level.lower() in ("region", "state"): # admin_level = 1 for key, value in d.iteritems(): if key not in ("admin_level","aliases","badnames","name","names","parent_pcode"): print "\tfor key", key, "value", value value = feature.get(value) print "\tvalue = ", value fields[key] = value geom = feature.geom geom.transform(u'+proj=longlat +datum=WGS84 +no_defs ') geos = geom.geos # transforms the geom to lat lon if necessary; if already longlat, nothing happens print "\tgeom_type = ", geom_type if isinstance(geos, Polygon): fields['mpoly'] = MultiPolygon([geos]) fields['point'] = geos.centroid print "centroid is", geos.centroid elif isinstance(geos, MultiPolygon): fields['mpoly'] = geos fields['point'] = geos.centroid elif isinstance(geos, Point): fields['point'] = geos #need to load lsibwvs for country polygons print i, "country_code is", country_code if country_code: if "country_code" not in fields: fields['country_code'] = country_code else: if "country_code" not in fields: try: fields['country_code'] = cc = Place.objects.get(admin_level=0, mpoly__contains=fields['point']).country_code country_codes.add(cc) except Exception as e: print e #raw_input() print "fields are", fields # look up place and see if one is nearby or whatever # qs = Place.objects.filter(name=name) # print "qs is", qs # for p in qs: # print "p is", p.__dict__ place = Place.objects.get_or_create(**fields)[0] if "parent_pcode" in d: parent_pcode = feature.get(d['parent_pcode']) print "parent_pcode is", parent_pcode qs = Place.objects.filter(pcode=parent_pcode) if qs.count() > 0: parent = qs[0] print "parent is", parent ParentChild.objects.get_or_create(parent=parent, child=place) for dic in d['aliases']: aliasString = feature.get(dic['name']) language = dic['language'] qs = Alias.objects.filter(alias=aliasString) if qs.count() == 0: alias = Alias.objects.create(alias=aliasString, language=language) print "created alias", alias.id else: alias = qs[0] alias.update({"language":language}) AliasPlace.objects.get_or_create(alias=alias, place=place)[0] print "created AliasPlace", alias, place places_added.append(place) except Exception as e: print "CAUGHT EXCEPTION on feature", i, "|", feature print e
class DistrictFactory(factory.Factory): FACTORY_FOR = models.District name = factory.Sequence(lambda n: u"District name %s" % n) geom = factory.Sequence(lambda _: MultiPolygon(Polygon.from_bbox(geom_district_iter.next()), srid=settings.SRID))
def test_couches_sig_link(self): # Fake restricted areas ra1 = RestrictedAreaFactory.create( geom=MultiPolygon(Polygon(((0, 0), (2, 0), (2, 1), (0, 1), (0, 0))))) ra2 = RestrictedAreaFactory.create( geom=MultiPolygon(Polygon(((0, 1), (2, 1), (2, 2), (0, 2), (0, 1))))) # Fake city c = City(code='005178', name='Trifouillis-les-marmottes', geom=MultiPolygon( Polygon(((0, 0), (2, 0), (2, 2), (0, 2), (0, 0)), srid=settings.SRID))) c.save() # Fake paths in these areas p = PathFactory( geom=LineString((0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5))) p.save() # This should results in 3 PathAggregation (2 for RA1, 1 for RA2, 1 for City) self.assertEquals(p.aggregations.count(), 4) self.assertEquals(p.topology_set.count(), 4) # PathAgg is plain for City t_c = c.cityedge_set.get().topo_object pa = c.cityedge_set.get().aggregations.get() self.assertEquals(pa.start_position, 0.0) self.assertEquals(pa.end_position, 1.0) # PathAgg is splitted for RA self.assertEquals(ra1.restrictedareaedge_set.count(), 2) self.assertEquals(ra2.restrictedareaedge_set.count(), 1) rae1a = ra1.restrictedareaedge_set.filter( aggregations__start_position=0).get() rae1b = ra1.restrictedareaedge_set.filter( aggregations__end_position=1).get() pa1a = rae1a.aggregations.get() pa1b = rae1b.aggregations.get() t_ra1a = rae1a.topo_object t_ra1b = rae1b.topo_object pa2 = ra2.restrictedareaedge_set.get().aggregations.get() t_ra2 = ra2.restrictedareaedge_set.get().topo_object self.assertAlmostEqual(pa1a.start_position, 0.0) self.assertAlmostEqual(pa1a.end_position, 0.5 / 3) self.assertAlmostEqual(pa1b.start_position, 2.5 / 3) self.assertAlmostEqual(pa1b.end_position, 1.0) self.assertAlmostEqual(pa2.start_position, 0.5 / 3) self.assertAlmostEqual(pa2.end_position, 2.5 / 3) # Ensure everything is in order after update p.geom = LineString((0.5, 0.5), (1.5, 0.5)) p.save() self.assertEquals(p.aggregations.count(), 2) self.assertEquals(p.topology_set.count(), 2) # Topology are re-created at DB-level after any update self.assertRaises(Topology.DoesNotExist, Topology.objects.get, pk=t_c.pk) self.assertRaises(Topology.DoesNotExist, Topology.objects.get, pk=t_ra1a.pk) self.assertRaises(Topology.DoesNotExist, Topology.objects.get, pk=t_ra1b.pk) self.assertRaises(Topology.DoesNotExist, Topology.objects.get, pk=t_ra2.pk) self.assertEquals(ra1.restrictedareaedge_set.count(), 1) # a new association exists for C t_c = c.cityedge_set.get().topo_object self.assertEquals(Topology.objects.filter(pk=t_c.pk).count(), 1) # a new association exists for RA1 t_ra1 = ra1.restrictedareaedge_set.get().topo_object self.assertEquals(Topology.objects.filter(pk=t_ra1.pk).count(), 1) pa1 = ra1.restrictedareaedge_set.get().aggregations.get() self.assertEquals(pa1.start_position, 0.0) self.assertEquals(pa1.end_position, 1.0) # RA2 is not connected anymore self.assertEquals(ra2.restrictedareaedge_set.count(), 0) self.assertEquals(Topology.objects.filter(pk=t_ra2.pk).count(), 0) # All intermediary objects should be cleaned on delete p.delete() self.assertEquals(c.cityedge_set.count(), 0) self.assertEquals(Topology.objects.filter(pk=t_c.pk).count(), 0) self.assertEquals(ra1.restrictedareaedge_set.count(), 0) self.assertEquals(Topology.objects.filter(pk=t_ra1.pk).count(), 0) self.assertEquals(ra2.restrictedareaedge_set.count(), 0) self.assertEquals(Topology.objects.filter(pk=t_ra2.pk).count(), 0)
class RestrictedAreaFactory(factory.Factory): FACTORY_FOR = models.RestrictedArea name = factory.Sequence(lambda n: u"Restricted area name %s" % n) geom = factory.Sequence(lambda _: MultiPolygon(Polygon.from_bbox(geom_area_iter.next()), srid=settings.SRID)) area_type = factory.SubFactory(RestrictedAreaTypeFactory)
def setUpTestData(cls): p1 = Polygon(((0, 0), (0, 1), (1, 1), (0, 0))) p2 = Polygon(((1, 1), (1, 2), (2, 2), (1, 1))) mp = MultiPolygon([p1, p2]) Location.objects.create(name="Metro Center", area=mp)
def setUp(self): self.team_member, created = TeamMember.objects.get_or_create( birth_year=1956, last_name='Krabappel') assert created second_admin_level_1 = SecondAdminLevel.objects.create( name_0='Country 1', name_1='State 1', name_2='County 1', varname_2='Springfield', mpoly=MultiPolygon( Polygon(((0, 0), (0, 100), (100, 100), (100, 0), (0, 0))), )) second_admin_level_1.save() second_admin_level_2 = SecondAdminLevel.objects.create( name_0='Country 1', name_1='State 1', name_2='County 2', varname_2='Shelbyville', mpoly=MultiPolygon( Polygon( ((0, 100), (0, 200), (100, 200), (100, 100), (0, 100))), )) second_admin_level_2.save() second_admin_level_3 = SecondAdminLevel.objects.create( name_0='Country 1', name_1='State 2', name_2='County 3', varname_2='Ogdenville', mpoly=MultiPolygon( Polygon(((300, 300), (300, 400), (400, 400), (400, 300), (300, 300))), )) second_admin_level_3.save() clusters = Clusters.objects.create(json={ "1": { "cluster_name": "Fast-Food Boulevard", "second_admin_level_name": "County 1", "first_admin_level_name": "State 1" }, "2": { "cluster_name": "Main Street", "second_admin_level_name": "County 1", "first_admin_level_name": "State 1" }, "3": { "cluster_name": "Chinatown", "second_admin_level_name": "County 2", "first_admin_level_name": "State 1" }, "4": { "cluster_name": "Manhattan Square", "second_admin_level_name": "County 2", "first_admin_level_name": "State 1" }, "5": { "cluster_name": "Ogdenville Outlet Mall", "second_admin_level_name": "County 3", "first_admin_level_name": "State 2" }, "6": { "cluster_name": "New Christiana", "second_admin_level_name": "County 3", "first_admin_level_name": "State 2" }, }, active=True) clusters.save() self.assertNotEqual(Clusters.get_active(), None)
def setup_chicago_sides(**kwargs): dt = models.DivisionType.objects.get(name='chicago_side') mappings = { 'Central': [ 'Near North Side', 'Loop', 'Near South Side', ], 'North': [ 'North Center', 'Lake View', 'Lincoln Park', 'Avondale', 'Logan Square' ], 'Far North': [ 'Rogers Park', 'West Ridge', 'Uptown', 'Lincoln Square', 'Edison Park', 'Norwood Park', 'Jefferson Park', 'Forest Glen', 'North Park', 'Albany Park', "OHare", 'Edgewater', ], 'Northwest': [ 'Portage Park', 'Irving Park', 'Dunning', 'Montclare', 'Belmont Cragin', 'Hermosa', ], 'West': [ 'Humboldt Park', 'West Town', 'Austin', 'West Garfield Park', 'East Garfield Park', 'Near West Side', 'North Lawndale', 'South Lawndale', 'Lower West Side' ], 'South': [ 'Armour Square', 'Douglas', 'Oakland', 'Fuller Park', 'Grand Boulevard', 'Kenwood', 'Washington Park', 'Hyde Park', 'Woodlawn', 'South Shore', 'Bridgeport', 'Greater Grand Crossing', ], 'Southwest': [ 'Garfield Ridge', 'Archer Heights', 'Brighton Park', 'McKinley Park', 'New City', 'West Elsdon', 'Gage Park', 'Clearing', 'West Lawn', 'Chicago Lawn', 'West Englewood', 'Englewood', ], 'Far Southeast': [ 'Chatham', 'Avalon Park', 'South Chicago', 'Burnside', 'Calumet Heights', 'Roseland', 'Pullman', 'South Deering', 'East Side', 'West Pullman', 'Riverdale', 'Hegewisch', ], 'Far Southwest': [ 'Ashburn', 'Auburn Gresham', 'Beverly', 'Washington Heights', 'Mount Greenwood', 'Morgan Park', ] } # not_found = collections.defaultdict(list) for k, v in mappings.items(): cas = [] # try to find all CAs for x in v: try: cas.append( models.ChicagoDivision.objects.get( type='chicago_community_area', name__iexact=x).mpoly) except Exception: # not_found[k].append(x) raise mpoly = reduce(lambda x, y: x.union(y), cas) try: m = models.ChicagoDivision.objects.get(type=dt, name=k) except models.ChicagoDivision.DoesNotExist: m = models.ChicagoDivision(type=dt, name=k) if isinstance(mpoly, Polygon): mpoly = MultiPolygon(mpoly) m.mpoly = mpoly m.save() if kwargs.pop('verbose', False): print "Created Chicago sides"
def photographer_create(request): if request.method == "POST": form = PhotographerForm(request.POST) if form.is_valid(): photographer = form.save(commit=False) photographer.user = request.user geometry = json.loads(photographer.geometry) multipolygon = MultiPolygon() for geo in geometry: coordinates = geo['geometry']['coordinates'][0] lineString = LineString() firstPoint = None for i in range(len(coordinates)): coor = coordinates[i] if i == 0: firstPoint = Point(coor[0], coor[1]) continue point = Point(coor[0], coor[1]) if i == 1: lineString = LineString(firstPoint.coords, point.coords) else: lineString.append(point.coords) polygon = Polygon(lineString.coords) multipolygon.append(polygon) photographer.multipolygon = multipolygon for geo in geometry: geo['properties']['photographer_id'] = str( photographer.unique_id) photographer.geometry = json.dumps(geometry) photographer.save() capture_types = form.cleaned_data['capture_type'] capture_method = form.cleaned_data['capture_method'] image_quality = form.cleaned_data['image_quality'] if capture_types is not None: photographer.capture_type.clear() for capture_type in capture_types: photographer.capture_type.add(capture_type) if capture_method is not None: photographer.capture_method.clear() for capture_m in capture_method: photographer.capture_method.add(capture_m) if image_quality is not None: photographer.image_quality.clear() for image_q in image_quality: photographer.image_quality.add(image_q) messages.success(request, 'A photographer was created successfully.') return redirect('photographer.index') else: form = PhotographerForm() content = { 'form': form, 'pageName': 'Create Photographer', 'pageTitle': 'Create Photographer' } return render(request, 'photographer/create.html', content)
def label_challenge_create(request): if request.method == "POST": form = LabelChallengeForm(request.POST) new_label_types = request.POST.get('new_label_types', '') if form.is_valid(): challenge = form.save(commit=False) challenge.user = request.user geometry = json.loads(challenge.geometry) multipolygon_ary = [] for geo in geometry: coordinates = geo['geometry']['coordinates'] print(coordinates) multipolygon_ary.append(Polygon(coordinates[0])) challenge.multipolygon = MultiPolygon(multipolygon_ary) for geo in geometry: geo['properties']['challenge_id'] = str(challenge.unique_id) challenge.geometry = json.dumps(geometry) challenge.save() label_type = form.cleaned_data['label_type'] if label_type is not None and len(label_type) > 0: challenge.label_type.clear() for lt in label_type: challenge.label_type.add(lt) if new_label_types is not None and new_label_types != '': label_types = new_label_types.split(',') if len(label_types) > 0: for label_type in label_types: l_ary = label_type.split('--') index = 0 if len(l_ary) > 0: l_type = None for l in l_ary: if index == 0: types = LabelType.objects.filter( parent__isnull=True, name=l) else: types = LabelType.objects.filter( parent=l_type, name=l) if types.count() == 0: l_parent_type = l_type l_type = LabelType() l_type.name = l l_type.parent = l_parent_type print(l_type.name) l_type.save() else: l_type = types[0] index += 1 if index == len(l_ary): challenge.label_type.add(l_type) messages.success(request, 'A label challenge was created successfully.') return redirect('challenge.my_label_challenge_list') else: form = LabelChallengeForm() label_types = LabelType.objects.filter(parent__isnull=True, source='mtpw') content = { 'form': form, 'pageName': 'Create Label Challenge', 'pageTitle': 'Create Label Challenge', 'label_types': label_types } return render(request, 'challenge/label/create.html', content)
def post(cls, request, ev_id=None): ev = request.data if ev_id: e = get_object_or_404(Event, pk=ev_id) else: e = Event() for k, v in ev.items(): if k in ['pk', 'players', 'owners', 'task_id', 'place', 'game']: continue if 'date' in k: try: v = datetime.datetime.strptime(v, '%Y-%m-%d %H:%M %z') except: v = parse_datetime(v) setattr(e, k, v) # setting the place place = ev.get('place', None) if place: if type(place) == str: place = GEOSGeometry(place) else: place = GEOSGeometry(str(place['geometry'])) if isinstance(place, Polygon): place = MultiPolygon(place) e.place = place # setting the game g = ev.get('game', None) if g: g = get_object_or_404(Game, pk=g['pk']) e.game = g e.save() e.owners.add(request.user) players_ids = [] for ep in ev.get('players', []): pk = ep['pk'] if pk < 0: # negative pk will create the player p = Player() p = cls.create_player(ep) else: p = Player.objects.get(pk=pk) p.about = ep['username'] p.set_position(*ep['pos']) players_ids.append(p.pk) e.set_playing(p, avoid_playing_event=True) Clue.objects.filter(player=p, event=e).delete() for ch in ep.get('challenges', []): c = Challenge.objects.get(pk=ch['pk'], games=e.game) clue = Clue(player=p, event=e, challenge=c, main=True) clue.save() players = e.players.exclude(membership__player__pk__in=players_ids) for p in players: p.user.delete() return Response({'status': 'ok'})
def convert_shp(self, shp_path, max_features=None): # shp = r"C:\arches\fpan\data\shp-ref\from_julie\AllPublicLandsFL_edit.shp" output = r"D:\arches\fpan\repo\fpan\fpan\fixtures\watermanagementdistricts.json" ds = DataSource(shp_path) layer = ds[0] lookup = { 'FL Dept. of Environmental Protection, Div. of Recreation and Parks': 'State Park', 'FL Dept. of Agriculture and Consumer Services, Florida Forest Service': 'State Forest', 'FL Fish and Wildlife Conservation Commission': 'Fish and Wildlife Conservation Commission', 'FL Dept. of Environmental Protection, Florida Coastal Office': 'Aquatic Preserve', 'FL Dept. of Environmental Protection, Office of Water Policy': 'Water Management District', } agencies = lookup.keys() feat_ct = 0 file_ct = 1 data = [] for feat in layer: agency = feat.get("MANAGING_A") if not agency in agencies: continue name = feat.get("MANAME") cat = lookup[agency] nickname = feat.get("NICKNAME") ## case specific handling of geometries 11-27-17 geotype = feat.geom.geom_type if geotype == "Polygon": geom = MultiPolygon(fromstr(feat.geom.wkt)).wkt if geotype == "MultiPolygon": geom = feat.geom.wkt f = { 'model': 'fpan.managedarea', 'pk': feat_ct, 'fields': { 'agency': agency, 'category': cat, 'geom': geom, 'name': name, 'nickname': nickname } } data.append(f) feat_ct += 1 if feat_ct % 100 == 0: print(feat_ct) elif feat_ct % 10 == 0: print(feat_ct, end="") ## write partial if max_features has been specified if max_features: if feat_ct % max_features == 0: output = output[:-6] + str(file_ct) + ".json" with open(output, "wb") as out: json.dump(data, out, indent=2) file_ct += 1 data = [] if not max_features: with open(output, "wb") as out: json.dump(data, out, indent=2)
reverse("v1:plan-detail", kwargs={"pk": plan.pk})) assert response.status_code == status.HTTP_204_NO_CONTENT response = api_client.get(reverse("v1:plan-detail", kwargs={"pk": plan.pk})) assert response.status_code == status.HTTP_404_NOT_FOUND @pytest.mark.django_db @pytest.mark.parametrize( "location,location_query,expected", ( (test_multi_polygon, test_polygon, 1), (test_multi_polygon, test_polygon_2, 1), (test_multi_polygon, test_polygon_3, 0), (MultiPolygon(test_polygon, srid=settings.SRID), test_polygon_2, 0), (MultiPolygon(test_polygon, srid=settings.SRID), test_polygon_3, 0), (MultiPolygon(test_polygon_2, srid=settings.SRID), test_polygon, 0), (MultiPolygon(test_polygon_2, srid=settings.SRID), test_polygon_3, 0), (MultiPolygon(test_polygon_3, srid=settings.SRID), test_polygon, 0), (MultiPolygon(test_polygon_3, srid=settings.SRID), test_polygon_2, 0), ), ) def test_plan_filter_location(location, location_query, expected): api_client = get_api_client() plan = get_plan(location=location) response = api_client.get(reverse("v1:plan-list"), {"location": location_query.ewkt}) assert response.status_code == status.HTTP_200_OK