def _store_em(self): [ism] = models.inputs4job(self.job.id, input_type="exposure") em = models.ExposureModel(owner=ism.owner, input=ism, name="AAA", category="single_asset", reco_type="aggregated", reco_unit="USD", stco_type="aggregated", stco_unit="USD") em.save() models.ExposureData(exposure_model=em, taxonomy="RC", asset_ref="A", number_of_units=100, stco=1, site=geos.GEOSGeometry(self.site.point.to_wkt()), reco=1).save() models.ExposureData(exposure_model=em, taxonomy="RM", asset_ref="B", number_of_units=40, stco=1, site=geos.GEOSGeometry(self.site.point.to_wkt()), reco=1).save() return em
def make_assets(self): [ism] = models.inputs4job(self.job.id, input_type="exposure") em = models.ExposureModel( owner=ism.owner, input=ism, name="AAA", category="single_asset", reco_type="aggregated", reco_unit="USD", stco_type="aggregated", stco_unit="USD") em.save() site_1 = shapes.Site(-116.0, 41.0) site_2 = shapes.Site(-117.0, 42.0) asset_1 = models.ExposureData( exposure_model=em, taxonomy="RC", asset_ref="asset_1", number_of_units=100, stco=1, site=geos.GEOSGeometry(site_1.point.to_wkt()), reco=1) asset_2 = models.ExposureData( exposure_model=em, taxonomy="RM", asset_ref="asset_2", number_of_units=40, stco=1, site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1) asset_3 = models.ExposureData( exposure_model=em, taxonomy="RM", asset_ref="asset_3", number_of_units=40, stco=1, site=geos.GEOSGeometry(site_2.point.to_wkt()), reco=1) asset_1.save() asset_2.save() asset_3.save() return asset_1, asset_2, asset_3
def get_context_data(self, **kwargs): context = super(DiaryDetail, self).get_context_data(**kwargs) context['staff'] = self.object.staff # get college plan for context # TODO add this routine as Location manager method try: value = Location.objects.get(name="CollegePlan").polygon context['college_wkt'] = geos.GEOSGeometry(value).wkt except Location.DoesNotExist: context['college_wkt'] = '' # initialize value value = None print(self.object, type(self.object)) for point in self.object.points(): if value is None: # initialize geosgeometry object value = geos.GEOSGeometry(point) else: # union of geosgeometry object is a multipoint # here need to group the tasks by task type value = value.union(geos.GEOSGeometry(point)) # create a wkt value suitable to pass to openmaps javascript # TODO place this routine somewhere wkt = '' if value: srid = 3857 if value.srid != srid: try: ogr = value.ogr ogr.transform(srid) wkt = ogr.wkt except gdal.OGRException: pass # wkt left as an empty string else: wkt = value.wkt context['diary_wkt'] = wkt try: context['next'] = self.object.get_next_by_day( staff=self.object.staff) except Diary.DoesNotExist: context['next'] = None try: context['previous'] = self.object.get_previous_by_day( staff=self.object.staff) except Diary.DoesNotExist: context['previous'] = None return context
def geoms_from_json(f, key=None): if hasattr(f, 'read'): d = json.load(f) else: d = json.loads(f) if key==None and 'geom' in d.itervalues().next(): key = 'geom' if key==None and 'geometry' in d.itervalues().next(): key = 'geometry' for k in d: if key: d[k] = geos.GEOSGeometry(json.dumps(d[k][key])) else: d[k] = geos.GEOSGeometry(json.dumps(d[k])) return d
def setUp(self): self.user = User.objects.create_user('user', '*****@*****.**', 'pw') from django.contrib.gis import geos # FIXME: use the bounding_geometry fixture for this self.bounding_box = geos.GEOSGeometry( '{"type":"MultiPolygon","coordinates":[[[[8.815935552120209,47.222220486817676],[8.815935552120209,47.22402752311505],[8.818982541561127,47.22402752311505],[8.818982541561127,47.222220486817676],[8.815935552120209,47.222220486817676]]]]}' ) self.excerpt = Excerpt.objects.create( name='Neverland', is_active=True, is_public=True, owner=self.user, bounding_geometry=self.bounding_box) extraction_order = ExtractionOrder.objects.create( excerpt=self.excerpt, orderer=self.user, process_id='53880847-faa9-43eb-ae84-dd92f3803a28', extraction_formats=['fgdb', 'spatialite'], coordinate_reference_system=4326, ) self.export = extraction_order.exports.get(file_format='fgdb') self.nonexistant_export_id = 999 self.assertRaises(ExtractionOrder.DoesNotExist, ExtractionOrder.objects.get, pk=self.nonexistant_export_id)
def _region_to_paths(region): if isinstance(region, dict): return _geojson_to_paths(region) elif isinstance(region, str): if region[0] == '{': # Treat as GeoJSON geojson = json.loads(region) return _geojson_to_paths(geojson) else: # Treat as WKT if HAS_SHAPELY: region = swkt.loads(region) elif HAS_GEODJANGO: region = geos.GEOSGeometry(region) else: raise NotImplementedError("Native WKT parsing not implemnted!") if HAS_SHAPELY: if isinstance(region, sg.Polygon): return _shapely_polygon_to_paths(region) elif isinstance(region, sg.MultiPolygon): exteriors = [] interiors = [] for p in region.geoms: exts, ints = _shapely_polygon_to_paths(p) exteriors.extend(exts) interiors.extend(ints) return exteriors, interiors if HAS_GEODJANGO: if isinstance(region, geos.Polygon): return _polygon_arr_to_paths(region.coords) elif isinstance(region, geos.MultiPolygon): return _mpolygon_arr_to_paths(region.coords) raise ValueError("Cannot convert to path. Invalid region: {}".format( repr(region)))
def retrieve(self, request, layer, coord): raw = [x for x in coord.split(',')] area_coord = ((raw[0], raw[1]), (raw[0], raw[3]), (raw[2], raw[3]), (raw[2], raw[1]), (raw[0], raw[1])) area_coord_str = ', '.join([' '.join(x) for x in area_coord]) area = geos.GEOSGeometry('POLYGON ((%s))' % area_coord_str) if settings.DISPLAY_DISTRICTS and int(layer) == 4: queryset = Polygon.objects.filter(Q(shape__bboverlaps=area) | Q(centroid__within=area), level__in=[3, 4]) else: queryset = Polygon.objects.filter(Q(shape__bboverlaps=area) | Q(centroid__within=area), level=int(layer)) print(queryset) search = self.request.query_params.get('search', None) if search: queryset = queryset.filter(address__icontains=search) # db_start = time.time() geodata = list(queryset.prefetch_related('organizations')) # print(' get polygon and orgs(sql)', time.time() - db_start) # serializer_start = time.time() serializer = PolygonSerializer(geodata, many=True) data = serializer.data # print(' serialization', time.time() - serializer_start) return Response(data)
def get_context(self, name, value, attrs=None, extra_context={}): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, basestring): try: value = geos.GEOSGeometry(value) except (geos.GEOSException, ValueError): value = None if (value and value.geom_type.upper() != self.geom_type and self.geom_type != 'GEOMETRY'): value = None # Defaulting the WKT value to a blank string wkt = '' if value: srid = self.map_srid if value.srid != srid: try: ogr = value.ogr ogr.transform(srid) wkt = ogr.wkt except gdal.OGRException: pass # wkt left as an empty string else: wkt = value.wkt context = super(BaseGeometryWidget, self).get_context(name, wkt, attrs) context['module'] = 'map_%s' % name.replace('-', '_') context['name'] = name context['ADMIN_MEDIA_PREFIX'] = settings.ADMIN_MEDIA_PREFIX context['LANGUAGE_BIDI'] = translation.get_language_bidi() return context
def get_context(self, name, value, attrs=None, extra_context={}, renderer=None): """Get for display the college polygon""" context = super(PointWidget, self).get_context(name, value, attrs, extra_context) # convert multipoint to geometry collection geos_obj = fromstr(context['value']) if isinstance(geos_obj, MultiPoint): context['value'] = GeometryCollection([p for p in geos_obj]).wkt elif isinstance(geos_obj, GeometryCollection): context['value'] = geos_obj.wkt else: context['value'] = GeometryCollection(geos_obj).wkt try: value = Location.objects.get(name="CollegePlan").polygon value = geos.GEOSGeometry(value) context['college_wkt'] = value.wkt except Location.DoesNotExist: context['college_wkt'] = '' return context
def parse_geo_json(geo_json): """Parses GeoJSON and returns a geometry object and metadata. :param geo_json: The geo json to parse :type geo_json: dict :rtype: GEOSGeometry, dict :returns: the geometry and metadata """ geom = None geom_json = None props = None if geo_json['type'] == 'Feature': geom_json = geo_json['geometry'] if 'properties' in geo_json: props = geo_json['properties'] elif geo_json['type'] == 'FeatureCollection': # Currently handles collections by just grabbing first entry geom_json = geo_json['features'][0]['geometry'] if 'properties' in geo_json['features'][0]: props = geo_json['features'][0]['properties'] else: # The GeoJSON is just a geometry geom_json = geo_json # Parse geometry if geom_json: try: geom = geos.GEOSGeometry(json.dumps(geom_json), srid=4326) except geos.GEOSException as geos_error: raise InvalidResultsManifest(str(geos_error)) return geom, props
def test_upload_field(self): geom = geos.GEOSGeometry(json.dumps(_geom)) fp = SimpleUploadedFile('up.json', geom.geojson.encode('ascii')) form = forms.RasterQueryForm({}, files={'upload': fp}) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['g'], geom.ogr) self.assertEqual(form.cleaned_data['g'].srs.srid, 4326)
def parse_geo_json(geo_json): '''Parses GeoJSON and returns a geometry object and metadata. :param geo_json: The geo json to parse :type geo_json: dict :rtype: GEOSGeometry, dict :returns: the geometry and metadata ''' geom = None geom_json = None props = None if geo_json[u'type'] == u'Feature': geom_json = geo_json[u'geometry'] if u'properties' in geo_json: props = geo_json[u'properties'] elif geo_json[u'type'] == u'FeatureCollection': # Currently handles collections by just grabbing first entry geom_json = geo_json[u'features'][0][u'geometry'] if u'properties' in geo_json[u'features'][0]: props = geo_json[u'features'][0][u'properties'] else: # The GeoJSON is just a geometry geom_json = geo_json # Parse geometry if geom_json: geom = geos.GEOSGeometry(json.dumps(geom_json), srid=4326) return geom, props
def extent(self, srid=None): """Returns the GeoQuerySet extent as a 4-tuple. The method chaining approach of geoqset.objects.transform(srid).extent() returns the extent in the original coordinate system, this method allows for transformation. Keyword args: srid -- EPSG id for for transforming the output geometry. """ if not srid and not connection.ops.spatialite: return super(GeoQuerySet, self).extent() transform = self._transform(srid) # Spatialite extent() is supported post-1.7. if connection.ops.spatialite: ext = {'extent': 'AsText(%s(%s))' % ('Extent', transform)} else: ext = {'extent': '%s(%s)' % (connection.ops.extent, transform)} # The bare order_by() is needed to remove the default sort field which # is not present in this aggregation. Empty querysets will return # [None] here. extent = (self.extra(select=ext).values_list('extent', flat=True).order_by()[0]) if not extent: return () try: try: # Django<=1.7 return connection.ops.convert_extent(extent) except TypeError: # Django 1.8 return connection.ops.convert_extent(extent, get_srid(self)) except NotImplementedError: return geos.GEOSGeometry(extent, srid).extent
def _process_qs(qs, obj_type="division"): count = qs.count() for i, obj in enumerate(qs): print("{} of {}: {} ({})".format( i, count, obj.name, getattr(obj, 'geography_curie', obj.format_geography_link()))) initial_req = requests.get(obj.format_geography_link()) geo_json_url = "{}.geojson".format(initial_req.url) req = requests.get(geo_json_url) if req.status_code != 200: print("Not found in MaPit: {}".format(initial_req.url)) continue json_data = req.text poly = self.clean_poly(geos.GEOSGeometry(json_data), transform=False, srid=4326) kwargs = { 'geography': poly, obj_type: obj, } DivisionGeography.objects.create(**kwargs) time.sleep(1)
def _process_qs(qs, obj_type="division"): count = qs.count() for i, obj in enumerate(qs): print("{} of {}: {} ({})".format( i, count, obj.name, getattr(obj, 'geography_curie', obj.format_geography_link()))) if obj_type == "organisation": code_type, code = ('gss', obj.gss) else: code_type, code = obj.geography_curie.split(':') code = str(code) if code_type == "gss": geo_json_url = "{}/{}.json".format( "http://statistics.data.gov.uk/boundaries", code) json_data = requests.get(geo_json_url).json()['geometry'] json_data = json.dumps(json_data) poly = self.clean_poly(geos.GEOSGeometry(json_data)) kwargs = { 'geography': poly, obj_type: obj, } DivisionGeography.objects.create(**kwargs) time.sleep(1)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) today = timezone.now() three_days_before = today + timedelta(days=-3) date_str = 'today' self.world_borders = WorldBorder.objects.all() self.active_country = self.world_borders.filter( name=self.active_country_name) self.active_country_geojson = serialize('geojson', self.active_country) self.reported_incidents = fds.objects.filter( geom__within=self.active_country[0].geom) self.recent_incidents = fds.objects.filter( date__range=[str(three_days_before), str(today)]) self.prone_ares = fdp.objects.all() geom = geos.GEOSGeometry(self.active_country[0].geom) context['center'] = geom.centroid.coords context['latitude'] = geom.centroid.coords[1] context['longitude'] = geom.centroid.coords[0] context['countries'] = self.world_borders context['selected_country'] = self.active_country[0] context['selected_country_geojson'] = self.active_country_geojson context['incidents'] = self.reported_incidents context['recent_incidents'] = self.recent_incidents context['prone_area'] = self.prone_ares return context
def get_context(self, name, value, attrs=None, extra_context={}): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.text_type): try: value = geos.GEOSGeometry(value) except (geos.GEOSException, ValueError): value = None value = None # Defaulting the WKT value to a blank string wkt = '' if value: srid = self.map_srid if value.srid != srid: try: ogr = value.ogr ogr.transform(srid) wkt = ogr.wkt except gdal.OGRException: pass # wkt left as an empty string else: wkt = value.wkt context = super(BaseGeometryWidget, self).get_context(name, wkt, attrs) context['module'] = 'map_%s' % name.replace('-', '_') context['name'] = name # Django >= 1.4 doesn't have ADMIN_MEDIA_PREFIX anymore, we must # rely on contrib.staticfiles. if hasattr(settings, 'ADMIN_MEDIA_PREFIX'): context['ADMIN_MEDIA_PREFIX'] = settings.ADMIN_MEDIA_PREFIX else: context['ADMIN_MEDIA_PREFIX'] = settings.STATIC_URL + 'admin/' context['LANGUAGE_BIDI'] = translation.get_language_bidi() return context
def _parse_collection(self, response, srid=None): data = json.loads(response.content) self.assertEqual(data['type'], 'FeatureCollection') self.assertEqual(len(data['features']), len(self.qs)) for feature in data['features']: yield geos.GEOSGeometry( json.dumps(feature['geometry']), srid or self.srid)
def compute_union_geometry(files, tolerance=0.0001): polygon_union = geos.GEOSGeometry('POINT (0 0)', srid=4326) count = 0 print('computing geometry union...') for f in files: print('file {}'.format(f)) try: ds = DataSource(f) except GDALException, e: traceback.print_exc() break for layer in ds: for feat in layer: print('processing feature {}'.format(count)) geom = geos.fromstr(feat.geom.wkt, srid=4326) if tolerance > 0: geom = geom.simplify(tolerance, preserve_topology=True) # Generalize to 'Multiploygon' if isinstance(geom, geos.Polygon): geom = geos.MultiPolygon(geom) if count == 0: polygon_union = geom.buffer(0) else: polygon_union = polygon_union.union(geom.buffer(0)) count += 1
def setUpClass(cls): cls.job = engine.prepare_job() jp, _, _ = engine.import_job_profile(RISK_DEMO_CONFIG_FILE, cls.job) cls.job_ctxt = helpers.create_job({}, job_id=cls.job.id, oq_job_profile=jp, oq_job=cls.job) calc = ClassicalRiskCalculator(cls.job_ctxt) calc.store_exposure_assets() [input] = models.inputs4job(cls.job.id, input_type="exposure") model = input.model() assets = model.exposuredata_set.filter(taxonomy="af/ctc-D/LR") # Add some more assets. coos = [(10.000155392289116, 46.546194318563), (10.222034128255, 46.0071299176413), (10.520376165581, 46.247463385278)] for lat, lon in coos: site = shapes.Site(lat, lon) cls.sites.append(site) if assets: continue location = geos.GEOSGeometry(site.point.to_wkt()) asset = models.ExposureData(exposure_model=model, taxonomy="af/ctc-D/LR", asset_ref=helpers.random_string(6), stco=lat * 2, site=location, reco=1.1 * lon) asset.save()
def get_geos_multipolygon(geom_str_repr): """ Retrieve a GEOS MultiPolygon from string """ geos_obj = geos.GEOSGeometry(geom_str_repr) if geos_obj and not isinstance(geos_obj, geos.MultiPolygon): return geos.MultiPolygon(geos_obj) return geos_obj
def learn(self, datasource): """ Trains the model using the provided data and targets. data is a NxM sized numpy matrix where N is the number of data points, and M the number of features. targets is a Nx1 sized numpy matrix with the N expected outcomes. """ data = [] targets = [] # Execute the query; gives back an array with all results rows = datasource.query(""" SELECT geom, elev, temperature FROM measurements, sensors WHERE measurements.sensor_id = sensors.id """) # Treat each row separately for row in rows: # The first element in the row is the point in WKT format # (e.g. 'POINT(-7.5 52.2)') and SRID 4326 (WGS84) # We need to project the point into SRID 900913 and extract # the coordinates geometry = geos.GEOSGeometry(row[0], 4326) # Project to SRID 900913 geometry.transform('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs') # We have now the coordinates of our point in the right SRS # We can insert a new row with x-coordinate, y-coordinate and # elevation in the data array, and the temperature into the target # array. data.append([geometry.x, geometry.y, float(row[1])]) targets.append([row[2]]) # Rescale the data and targets between 0 and 1 data = np.array(data) self.data_min = [] self.data_max = [] pts, dims = data.shape for i in range(0, dims): self.data_min.append(np.min(data[:,i])) self.data_max.append(np.max(data[:,i])) data[:,i] = (data[:,i] - self.data_min[i]) / (self.data_max[i] - self.data_min[i]) targets = np.array(targets) self.target_min = np.min(targets) self.target_max = np.max(targets) targets = (targets - self.target_min) / (self.target_max - self.target_min) # Initialize the KRLS algorithm self.kp = i2maps.contrib.krls.KRLS( kernel=self.kernel, adopt_thresh=self.ald_thresh, state=data[0,:], target=targets[0], maxsize=self.maxsize, adaptive=self.adaptive, forget_rate=self.forget_rate ) # Learn the model (sample by sample) for i in range(1, len(data)): self.kp.update(np.array(data[i,:]), np.array(targets[i]))
def hydrate_geom(self, bundle): try: geom = bundle.data.get('geom') boundary = geos.GEOSGeometry(json.dumps(geom)) if type(boundary) is geos.Polygon: bundle.data['geom'] = json.loads(geos.MultiPolygon(boundary).json) except: bundle.data['geom'] = None return bundle
def hydrate_district(self, bundle): try: geom = bundle.data.get('district') boundary = geos.GEOSGeometry(json.dumps(geom)) if type(boundary) is geos.Polygon: boundary = geos.MultiPolygon(boundary) bundle.data['district'] = json.loads(boundary.json) except Exception: bundle.data['district'] = None return bundle
def to_representation(self, instance): feature = OrderedDict() feature["id"] = instance.event_id feature["type"] = "Feature" feature["geometry"] = GeoJsonDict( json.loads(geos.GEOSGeometry(instance.geom).envelope.geojson)) further_info = self.context["events_info"][str(instance.event_id)] feature["properties"] = self.get_feature_properties( instance, further_info) return feature
def geoms_from_shapefile(filename, id=None): ds = gdal.DataSource(filename) layer = ds[0] id_column = lambda f: f.get(id) if id else f.fid geoms = {} for f in layer: try: geoms[id_column(f)] = geos.GEOSGeometry(f.geom.wkt) except Exception, e: print e
def test_simplify_geojson(self): fn = functions.AsGeoJSON(query.Simplify( functions.Transform('geom', self.srid), self.tol), precision=2) sqs = self.qs.all().annotate(geojson=fn) geom = geos.GEOSGeometry(sqs[0].geojson, self.srid) source = self.qs[0].geom self.assertNotEqual(geom, source) self.assertNotEqual(geom.srid, source.srid) self.assertLess(geom.num_coords, source.num_coords)
def _get_overrides(self, identifier=None, size=None, extent=None, begin_time=None, end_time=None, footprint=None, projection=None, coverage_type=None, srid=None, **kwargs): overrides = {} if coverage_type: overrides["coverage_type"] = coverage_type if identifier: overrides["identifier"] = identifier if extent: overrides["extent"] = map(float, extent.split(",")) if size: overrides["size"] = map(int, size.split(",")) if begin_time: overrides["begin_time"] = parse_datetime(begin_time) if end_time: overrides["end_time"] = parse_datetime(end_time) if footprint: footprint = geos.GEOSGeometry(footprint) if footprint.hasz: raise CommandError( "Invalid footprint geometry! 3D geometry is not supported!" ) if footprint.geom_type == "MultiPolygon": overrides["footprint"] = footprint elif footprint.geom_type == "Polygon": overrides["footprint"] = geos.MultiPolygon(footprint) else: raise CommandError( "Invalid footprint geometry type '%s'!" % (footprint.geom_type) ) if projection: try: overrides["projection"] = int(projection) except ValueError: overrides["projection"] = projection elif srid: try: overrides["projection"] = int(srid) except ValueError: pass return overrides
def test_from_request(self): request = factory.post('/', json.dumps({'g': _geom}), content_type='application/json') view = APIView() request = view.initialize_request(request) view.initial(request) form = forms.RasterQueryForm.from_request(request) self.assertTrue(form.is_valid()) geom = geos.GEOSGeometry(json.dumps(_geom)) self.assertEqual(form.cleaned_data['g'], geom.ogr)
def start_search_screen(request): if request.user.is_authenticated() and not request.user.venueuser.location: return redirect('profile-form', pk=request.user.venueuser.pk) if 'lat' in request.GET and 'lon' in request.GET: return closest(request) form = forms.AddressForm() restaurants = [] latitude = "" longitude = "" address = "" name = "" if request.POST: page = request.POST.get('page') form = forms.AddressForm(request.POST) if form.is_valid(): address = form.cleaned_data['address'] name = form.cleaned_data['name'] if not address: restaurants = Restaurant.objects.filter(approved=True, is_suspended=False, name__icontains=name) try: longitude, latitude = restaurants[0].location except IndexError: pass else: geocoder = GoogleV3(api_key=GOOGLE_KEY) try: location = geocoder.geocode(address) except Exception as e: print e location = "" if location: latitude = location.latitude longitude = location.longitude currentPoint = geos.GEOSGeometry('POINT(%s %s)' % (longitude, latitude)) distance_m = {'km': 30} restaurants = Restaurant.gis.filter(approved=True, location__distance_lte=(currentPoint, measure.D(**distance_m)), name__icontains=name).distance(currentPoint).order_by('distance') context = {'all_restaurants': restaurants, 'form': form, 'longitude': longitude, 'latitude': latitude, 'address': address, 'name': name} return render(request, 'restaurants/restaurants.html', context) # paginator = Paginator(restaurants, 20) # page = request.GET.get("page") # try: # restaurants = paginator.page(page) # except PageNotAnInteger: # restaurants = paginator.page(1) # except EmptyPage: # restaurants = paginator.page(paginator.num_pages) # # context = {'all_restaurants': restaurants, 'form': form, 'longitude': longitude, 'latitude': latitude, 'address': address, 'name': name} return render(request, 'restaurants/start_search.html')