def setupTreemapEnv(): def local_render_to_response(*args, **kwargs): from django.template import loader from django.http import HttpResponse hr = HttpResponse(loader.render_to_string(*args, **kwargs)) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response instance = make_instance(is_public=True) create_stewardship_udfs(instance) make_user_with_default_role(instance, 'jim') commander = make_commander_user(instance, 'commander') make_apprentice_user(instance, 'apprentice') 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 = Boundary(name="n1", category='blah', sort_order=4, geom=n1geom) n2 = Boundary(name="n2", category='blah', sort_order=4, geom=n2geom) n1.save() n2.save() s1 = Species(otm_code="s1", genus="testus1", species="specieius1", cultivar='', instance=instance) s2 = Species(otm_code="s2", genus="testus2", species="specieius2", cultivar='', instance=instance) s3 = Species(otm_code="s3", genus="testus2", species="specieius3", cultivar='', instance=instance) s1.is_native = True s1.fall_conspicuous = True s1.flower_conspicuous = True s1.palatable_human = True s2.is_native = True s2.fall_conspicuous = False s2.flower_conspicuous = True s2.palatable_human = False s2.has_wildlife_value = True s3.has_wildlife_value = True s1.save_with_user(commander) s2.save_with_user(commander) s3.save_with_user(commander) return instance
def _spatial_extent_(self): min_x = float(self.dataset.variables[self.colbnds_name][:].min()) min_y = float(self.dataset.variables[self.rowbnds_name][:].min()) max_x = float(self.dataset.variables[self.colbnds_name][:].max()) max_y = float(self.dataset.variables[self.rowbnds_name][:].max()) p = Polygon(((min_x,min_y),(max_x,min_y),(max_x,max_y),(min_x,max_y),(min_x,min_y)),srid=4326) return(p)
def test_simple_inclusion(self): self.rc.region_constraint = Polygon( ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) results = models.ExposureData.objects.get_asset_chunk( self.rc, "test", 0, 10) self.assertEqual(1, len(list(results))) self.assertEqual("test1", results[0].asset_ref)
def test_inclusion_of_a_pole(self): self.rc.region_constraint = Polygon( ((-1, 0), (-1, 1), (1, 1), (1, 0), (-1, 0))) results = models.ExposureData.objects.get_asset_chunk( self.rc, "test", 0, 10) self.assertEqual(1, len(results)) self.assertEqual("test1", results[0].asset_ref) self.rc.region_constraint = Polygon( ((179, 10), (-179, 10), (-179, -10), (179, -10), (179, 10))) results = models.ExposureData.objects.get_asset_chunk( self.rc, "test", 0, 10) self.assertEqual(1, len(list(results))) self.assertEqual("test2", results[0].asset_ref)
def _spatial_extent_(self): spatial_extent = self.archive_meta['spatial_extent'] min_x = float(spatial_extent['min_x']) max_x = float(spatial_extent['max_x']) min_y = float(spatial_extent['min_y']) max_y = float(spatial_extent['max_y']) p = Polygon(((min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y), (min_x, min_y)), srid=4326) return (p)
def create_parking_1(save=True): parking = Parking(name='Karlovo namesti', places_count=200, land_registry_number='100', street='Husova 5', city='Praha', polygon=Polygon(((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)))) if save: parking.save() return parking
def get_cars(**kwargs): manufacturer, created = CarModelManufacturer.objects.get_or_create( slug='slug', name='title') type, created = CarType.objects.get_or_create(type='type') fuel, created = Fuel.objects.get_or_create(title='title') color, created = CarColor.objects.get_or_create(color='blue') car_model, created = CarModel.objects.get_or_create( engine='engine', seats_count=1, storage_capacity=1, name='title', type=type, main_fuel=fuel, manufacturer=manufacturer) defaults = { 'active': True, 'manufacture_date': datetime.now(), 'model': car_model, 'color': color, 'home_subsidiary': get_subsidiary(), } defaults.update(**kwargs) try: car = Car.objects.get(registration_number='123123') except Car.DoesNotExist: car = Car.objects.create(registration_number='123123', **defaults) parking = Parking.objects.get_or_create(name='Test', places_count=200, land_registry_number='100', street='Test', city='Praha', polygon=Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)))) return car_model, car, parking
def convert2pgsql(self) -> List[PlanetOsmPolygon]: # https://wiki.openstreetmap.org/wiki/Relation:multipolygon/Algorithm previous_timestamp: date = self.timestamp rel: PlanetOsmRels multipolygons: List[PlanetOsmPolygon] = [] for rel in self.rels: if (not rel.visible or not rel.outer_members or not rel.inner_members or not rel.tags): if rel.timestamp: previous_timestamp = rel.timestamp continue if rel.rel_type != "multipolygon" and rel.rel_type != "boundary": continue ways: Dict[int, PlanetOsmWays] = {} way: PlanetOsmWays for way in PlanetOsmWays.objects.filter( osm_id__in=rel.outer_members, visible=True).order_by("osm_id", "-version"): if not way.way or not way.way.closed: # todo combine not closed ways continue if way.osm_id in ways: if way.timestamp <= ways[way.osm_id].timestamp: ways[way.osm_id] = way else: ways[way.osm_id] = way for way in PlanetOsmWays.objects.filter( osm_id__in=rel.inner_members, visible=True).order_by("osm_id", "-version"): if not way.way or not way.way.closed: # todo combine not closed ways continue if way.osm_id in ways: if way.timestamp <= ways[way.osm_id].timestamp: ways[way.osm_id] = way else: ways[way.osm_id] = way polygons: List[Polygon] = [] for osm_id in rel.outer_members: if osm_id in ways: polygons.append(Polygon(ways[osm_id].way.coords)) for osm_id in rel.inner_members: if osm_id in ways: polygons.append(Polygon(ways[osm_id].way.coords)) multipolygon: MultiPolygon = MultiPolygon(polygons) polygon: PlanetOsmPolygon = PlanetOsmPolygon( osm_id=rel.osm_id, version=rel.version, way=GEOSGeometry(multipolygon.wkt), valid_since=rel.timestamp, valid_until=previous_timestamp, tags=rel.tags, ) polygon = fill_osm_object(osm_object=polygon) multipolygons.append(polygon) previous_timestamp = rel.timestamp self.rels.clear() self.osm_id = None return multipolygons
def setupTreemapEnv(): settings.GEOSERVER_GEO_LAYER = "" settings.GEOSERVER_GEO_STYLE = "" settings.GEOSERVER_URL = "" 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 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") for r in [r1, r2, r3, r4, r5, r6, r7, r8]: r.save() 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() dbh = "[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]" dbh2 = "[2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]" rsrc1 = Resource(meta_species="BDM OTHER", region="NoEastXXX") rsrc2 = Resource(meta_species="BDL OTHER", region="NoEastXXX") rsrc1.save() rsrc2.save() 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) up.save() u.reputation = Reputation(user=u) u.reputation.save() amy_filter_result = User.objects.filter(username="******") if not amy_filter_result: amy = User.objects.create_user("amy", "*****@*****.**", "amy") else: amy = amy_filter_result[0] amy.is_staff = False amy.is_superuser = False amy.save() amy_profile = UserProfile(user=amy) amy_profile.save() amy.reputation = Reputation(user=amy) amy.reputation.save() olivia_filter_result = User.objects.filter(username="******") if not amy_filter_result: olivia = User.objects.create_user("olivia", "*****@*****.**", "olivia") else: olivia = olivia_filter_result[0] olivia.is_staff = False olivia.is_superuser = False olivia.save() olivia_profile = UserProfile(user=olivia) olivia_profile.save() olivia.reputation = Reputation(user=olivia) olivia.reputation.save() 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() s1 = Species(symbol="s1", genus="testus1", species="specieius1", cultivar_name='', family='', alternate_symbol='a1') s2 = Species(symbol="s2", genus="testus2", species="specieius2", cultivar_name='', family='', alternate_symbol='a2') s3 = Species(symbol="s3", genus="testus2", species="specieius3", cultivar_name='', family='', alternate_symbol='a3') s1.native_status = 'True' s1.fall_conspicuous = True s1.flower_conspicuous = True s1.palatable_human = True s2.native_status = 'True' s2.fall_conspicuous = False s2.flower_conspicuous = True s2.palatable_human = False s2.wildlife_value = True s3.wildlife_value = True s1.save() s2.save() s3.save() s1.resource.add(rsrc1) s2.resource.add(rsrc2) s3.resource.add(rsrc2) ie = ImportEvent(file_name='site_add') ie.save()
def handle(self, **options): '''This method takes a given shape names and queries the usgs api for available scenes. Using two api clients for the usgs and espa we query for a given shape and create an order to download the landsat scenes for a specific temporal window. ''' usgs_client = UsgsApi() usgs_client.login() start_date = options['start_date'][0] end_date = options['end_date'][0] landsat = int(options['landsat'][0]) shape_name = options['shape'][0] cloud_cover = options['max_cloud_cover'] espa_client = EspaApi() logger.info(shape_name) try: shape_object = Country.objects.get(name=shape_name) logger.info('Country %s was loaded.' % shape_name) except: try: shape_object = Region.objects.get(name=shape_name) logger.info('Region %s was loaded.' % shape_name) except: shape_object = None if shape_object: extent = shape_object.the_geom.extent if landsat == 8: collection_usgs = 'LANDSAT_8_C1' collection_espa = 'olitirs8_collection' collection_regex = '^lc08_{1}\\w{4}_{1}[0-9]{6}_{1}[0-9]{8}_{1}[0-9]{8}_{1}[0-9]{2}_{1}\\w{2}$' elif landsat == 7: collection_usgs = 'LANDSAT_ETM_C1' collection_espa = 'etm7_collection' collection_regex = '^le07_{1}\\w{4}_{1}[0-9]{6}_{1}[0-9]{8}_{1}[0-9]{8}_{1}[0-9]{2}_{1}\\w{2}$' elif landsat == 5: collection_usgs = 'LANDSAT_TM_C1' collection_espa = 'tm5_collection' collection_regex = '^lt05_{1}\\w{4}_{1}[0-9]{6}_{1}[0-9]{8}_{1}[0-9]{8}_{1}[0-9]{2}_{1}\\w{2}$' data = usgs_client.search(extent, collection_usgs, start_date=start_date, end_date=end_date, max_cloud_cover=cloud_cover).get('data') products = ['sr', 'pixel_qa'] interest = [] if data: results = data.get('results') if results: for scene in results: coords = tuple( point_from_object(scene.get(coord)) for coord in [ 'lowerLeftCoordinate', 'upperLeftCoordinate', 'upperRightCoordinate', 'lowerRightCoordinate', 'lowerLeftCoordinate' ]) scene_extent = Polygon(coords) entity_id = scene.get('displayId') # we use the same regular expression that espa uses to filter the names that are valid; otherwise, the order throws an error if scene_extent.intersects( shape_object.the_geom) and re.match( collection_regex, entity_id.lower()): interest.append(entity_id) footprint, _ = Footprint.objects.get_or_create( name=entity_id, the_geom=scene_extent) print(json.dumps(interest, indent=4)) data = espa_client.order(collection_espa, interest, products) if data.get('status') == 'ordered': logger.info('The order was posted with id: %s' % data.get('orderid')) order = Order(user=espa_client.username, order_id=data.get('orderid'), downloaded=False) order.save() else: logger.info(json.dumps(data, indent=4)) else: logger.info( 'No shape with the name %s was found in the database.' % shape_name)