def retrive_bikes(self, lat, lng): bike_objects = [] # Retrieve ofo bikes pyofo.set_token(settings.OFO_TOKEN) ofo = pyofo.Ofo() ro = ofo.nearby_ofo_car(lat=lat, lng=lng) ofo_data = json.loads(ro.text) ofo_bikes = ofo_data.get('values', {}).get('cars', []) for ofo_bike in ofo_bikes: bike_objects.append(Bike(bike_id=ofo_bike.get("carno"), company="ofo", lng=ofo_bike.get("lng"), lat=ofo_bike.get("lat"), data=ofo_bike, gis_location=geos.Point(ofo_bike.get("lng"), ofo_bike.get("lat")))) # Retrieve mobike bikes rm = requests.post('https://mwx.mobike.com/mobike-api/rent/nearbyBikesInfo.do', data='latitude=%s&longitude=%s' % (lat, lng), headers={ 'content-type': 'application/x-www-form-urlencoded', 'user-agent': 'Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/60.0.3112.107 ' 'Mobile Safari/537.36' }) mobike_data = json.loads(rm.text) mobike_bikes = mobike_data.get('object', []) for mobike_bike in mobike_bikes: bike_objects.append(Bike(bike_id=mobike_bike.get("bikeIds"), company="mobike", lng=mobike_bike.get("distX"), lat=mobike_bike.get("distY"), data=mobike_bike, gis_location=geos.Point(mobike_bike.get("distX"), mobike_bike.get("distY")))) if bike_objects: Bike.objects.bulk_create(bike_objects)
def test_routing(self): # test signal outside center signal_outside = SignalFactory.create( location__geometrie=geos.Point(1.0, 1.0) ) signal_outside.refresh_from_db() self.assertIsNone(signal_outside.routing_assignment) # simulate apply routing rules self.dsl_service.process_routing_rules(signal_outside) signal_outside.refresh_from_db() self.assertIsNone(signal_outside.routing_assignment) # test signal inside center signal_inside = SignalFactory.create( location__geometrie=geos.Point(4.88, 52.36) ) signal_inside.refresh_from_db() self.assertIsNone(signal_inside.routing_assignment) # simulate apply routing rules self.dsl_service.process_routing_rules(signal_inside) signal_inside.refresh_from_db() self.assertIsNotNone(signal_inside.routing_assignment) self.assertEqual(len(signal_inside.routing_assignment.departments.all()), 1) routing_dep = signal_inside.routing_assignment.departments.first() self.assertEqual(routing_dep.id, self.department.id)
def test_export_csv_synonyms(self): """ test exporting a place with synonyms """ assert test_client_login(self.client, username='******', password='******') == True place_type = PlaceType.objects.get(slug='poi') place = Place(pretty_name='Donut Palace', place_type=place_type, address='100 Bubble Street', location=geos.Point(1.0, 2.0)) place.save() ps = PlaceSynonym(pretty_name='Donut Hole', place=place) ps.save() ps = PlaceSynonym(pretty_name='Donut Pally', place=place) ps.save() place = Place(pretty_name='Donut Sanctuary', place_type=place_type, address='101 Bubble Street', location=geos.Point(3.0, 4.0), url='http://www.example.org/bs') place.save() ps = PlaceSynonym(pretty_name='Sancy D', place=place) ps.save() ps = PlaceSynonym(pretty_name='D Sanc', place=place) ps.save() response = self.client.post(self.export_url, {'place_type': place_type.id}) self.assertEqual(response.status_code, 200) rows = csv.reader(StringIO(response.content)) count = 0 for row in rows: self.assertEqual(len(row), 7) synonyms = set(row[5:]) if row[0] == 'Donut Palace': self.assertEqual(row[1], '100 Bubble Street') self.assertEqual(row[2], '2.0') self.assertEqual(row[3], '1.0') self.assertEqual(row[4], '') assert 'Donut Hole' in synonyms assert 'Donut Pally' in synonyms elif row[0] == 'Donut Sanctuary': self.assertEqual(row[1], '101 Bubble Street') self.assertEqual(row[2], '4.0') self.assertEqual(row[3], '3.0') self.assertEqual(row[4], 'http://www.example.org/bs') assert 'Sancy D' in synonyms assert 'D Sanc' in synonyms else: self.fail('Unexpected Place!' % row[0]) count += 1 self.assertEqual(count, 2)
def lon_bounds_srid(srid): '''Return a tuple containing the X coordinates of the (west, east) "boundaries" of the given SRID. This is a little arbitrary, because there's no boundary (all the trigonometry works fine even if you go around and around the world), but it's useful to be able to make a boundary rectangle. Thus, we add a generous buffer.''' xmin = transform(geos.Point(LONMIN, 0, srid=SRID_WGS84), srid).x xmax = transform(geos.Point(LONMAX, 0, srid=SRID_WGS84), srid).x return (xmin * LON_BUFFER, xmax * LON_BUFFER)
def point(faker, field, srid): lat = random.uniform(-180.0, 180.0) lng = random.uniform(-90, 90) if field.dim == 2: return geos.Point(lat, lng, srid=srid) else: alt = random.uniform(-4000.0, 9000.0) return geos.Point(lat, lng, alt, srid=srid)
def lat_bounds_srid(srid): '''Return a tuple containing the Y coordinates of the (south, north) poles. For example: >>> lat_bounds_srid(4326) (-89.99, 89.99) >>> lat_bounds_srid(54003) (-14671436.0..., 14671436.0...)''' return (transform(geos.Point(0, LATMIN, srid=SRID_WGS84), srid).y, transform(geos.Point(0, LATMAX, srid=SRID_WGS84), srid).y)
def test_index_page_with_two_marker_shows_full_extent(self): models.Marker.objects.create(name='M1', location=geos.Point(9.65, 76.25)) models.Marker.objects.create(name='M2', location=geos.Point(9.75, 76.5)) response = self.client.get('/') self.assertContains(response, 'M1') self.assertContains(response, 'M2') self.assertContains(response, json.dumps([[9.65, 76.25], [9.75, 76.5]])) self.assertEquals(response.context['js_constants']['extent_points'], [[9.65, 76.25], [9.75, 76.5]])
def setUp(self): ParkingSpot.objects.create( code='A', is_reserved=True, point=geo_models.Point(5, 23), current_base_cost=10, ) ParkingSpot.objects.create( code='B', is_reserved=False, point=geo_models.Point(5, 29), current_base_cost=20, ) USER.objects.create_user(CONTACT_NUMBER, PASSWORD)
def build(self): self.warn_if_parallel() UTM_18N = 32618 # work in UTM for sane units (meters) on buffering manhattan = geos.Point((584939, 4509087), srid=UTM_18N) manhattan_poly = manhattan.buffer(9500) brooklyn_poly = geos.Point((588618, 4497627), srid=UTM_18N).buffer(5000) bronx_poly = geos.Point((593911, 4521284), srid=UTM_18N).buffer(2000) queens_poly = geos.Point((595675, 4511623), srid=UTM_18N).buffer(1000) multiregion = geos.MultiPolygon((manhattan_poly, brooklyn_poly, bronx_poly, queens_poly), srid=manhattan_poly.srid) self.pred_region = multiregion.cascaded_union self.best_point = manhattan
def save(joined_data, gps_collection_file, pollutant_collection_file, pollutant): """Save a table of joined data to the database Parameters ---------- joined_data : pandas DataFrame gps_collection_file : woeip.apps.air_quality.models.CollectionFile CollectionFile object for GPS data pollutant_collection_file : woeip.apps.air_quality.models.CollectionFile CollectionFile object for pollutant data pollutant : woeip.apps.air_quality.models.Pollutant Pollutant that was collected """ for _, row in joined_data.iterrows(): time_geo = models.TimeGeo( collection_file=gps_collection_file, time=row["time"], location=geos.Point(row["lon"], row["lat"]), ) time_geo.save() pollutant_value = models.PollutantValue( collection_file=pollutant_collection_file, time_geo=time_geo, pollutant=pollutant, value=row["measurement"], ) pollutant_value.save()
def cae(token, points, token_gmms): m = token_gmms[token] m.prepare(0.95) av = np.average(points, axis=0) avg_p = geos.Point(*av, srid=points[0].srid) w = 1 / (1 + m.cae(avg_p)) return w
def clean(self): cleaned_data = super(PointImportForm, self).clean() lat, lon = cleaned_data.get('latitude'), cleaned_data.get('longitude') # point geometry geometry = cleaned_data.get('geometry') if not geometry: if not all((lat, lon)): raise forms.ValidationError( 'Geometry fields require latitude and longitude') geometry_field = self.fields['geometry'] pnt = geos.Point(lon, lat, srid=geometry_field.srid) cleaned_data['geometry'] = geometry_field.clean(pnt) # zipcode geocode zipcode = cleaned_data.get('zip') if not zipcode: if not all((lat, lon)): raise forms.ValidationError( 'Zipcode fields require latitude and longitude') key = hash((lat, lon)) result_data = self._cache.get(key) if result_data: result = GeocoderResult(result_data) else: result = Geocoder.reverse_geocode(lat=lat, lng=lon) self._cache.set(key, result.data) zipcode = result[0].postal_code cleaned_data['zip'] = self.fields['zip'].clean(zipcode) return cleaned_data
def _create_event_record(status_change): properties = {"trip_id": status_change.get("associated_trip")} event_location = status_change["event_location"] if event_location: # GeoJSON Point Feature try: longitude, latitude, altitude = event_location["geometry"][ "coordinates"] except ValueError: longitude, latitude = event_location["geometry"]["coordinates"] altitude = None point = geos.Point(longitude, latitude, altitude, srid=4326) properties["telemetry"] = { "timestamp": event_location["properties"]["timestamp"], "gps": { "lng": longitude, "lat": latitude }, # No coordinates, no battery charge saved "battery_pct": status_change.get("battery_pct"), } if altitude: properties["telemetry"]["gps"]["altitude"] = altitude else: # Spec violation! point = None return models.EventRecord( device_id=status_change["device_id"], timestamp=utils.from_mds_timestamp(status_change["event_time"]), point=point, event_type=status_change["agency_event_type"], properties=properties, )
def value_from_datadict(self, data, files, name): lat, lon = [widget.value_from_datadict(data, files, f'{name}_{i}') for i, widget in enumerate(self.widgets)] if not lat or not lon: return None location = geos.Point(float(lat), float(lon)) return str(location)
class EventRecord(factory.DjangoModelFactory): class Meta: model = models.EventRecord device = factory.SubFactory(Device) timestamp = factory.Sequence(lambda n: datetime.datetime( 2018, 8, 1, tzinfo=pytz.utc) + datetime.timedelta(seconds=n)) point = factory.LazyAttribute(lambda f: geos.Point( f.properties["telemetry"]["gps"]["lng"], f.properties["telemetry"]["gps"]["lat"], )) saved_at = datetime.datetime(2018, 8, 1, 1, tzinfo=pytz.utc) event_type = factory.Iterator(c.name for c in enums.EVENT_TYPE) properties = factory.Dict({ "trip_id": None, "telemetry": factory.Dict({ "timestamp": 1_325_376_000_000, "gps": factory.Dict({ "lat": 0.0, "lng": 3.0, "altitude": 30.0, "heading": 245.2, "speed": 32.3, "accuracy": 2.0, }), "battery_pct": 0.5, }), })
def test_place_filters(self): datetimes = (datetime.datetime(*d) for d in ((2100, 1, 2, 0, 0), (2100, 1, 1, 12, 00), (2100, 1, 1, 23, 00))) place = get(Place, point=F(geometry=geos.Point(x=0, y=0))) matched_events = { self.make_event_fixture(start_date=dt.date(), start_time=dt.time(), place=place) for dt in datetimes } self.make_event_fixture() # throw in a non-match query_params = dict(**self.auth_params) query_params.update( **{'place': PlaceFilterOptions()._uri_from_obj(place)}) resp = self.client.get(self.uri, data=query_params) self.assertResponseCode(resp, 200) self.assertResponseMetaList(resp, len(matched_events)) json_resp = try_json_loads(resp.content) resource = api_v1._registry['eventsummary'] matched_events_sorted = sorted( matched_events, key=lambda e: e.occurrences.all()[0].start_datetime) for matched_event, event_summary_json in zip(matched_events_sorted, json_resp['objects']): event_summary = resource.get_via_uri( event_summary_json['resource_uri']) self.assertIn(event_summary.event, matched_events, 'Should not be part of search results') self.assertEqual(matched_event, event_summary.event, 'Unexpected order')
def test_detail_get_future(self): occ = get(Occurrence, start_date=datetime.datetime.now().date() + datetime.timedelta(days=30), place=F(point=F(geometry=geos.Point(y=0, x=0)))) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNotNone(resp_dict, 'Malformed response') self.assertResponseCode(resp, 200) self.assertEqual(uri, resp_dict['resource_uri'], 'Unexpected resource uri in response') event_field = resp_dict.get('event') self.assertIsInstance( event_field, basestring, 'Did not find expected reference to associated event URI') place_field = resp_dict.get('place') self.assertIsInstance(place_field, dict, 'Full occurrence does not have place dict') point_field = place_field.get('point') self.assertIsInstance(point_field, dict, 'Full occurrence dict does not have point dict') city_field = point_field.get('city') self.assertIsInstance(city_field, dict, 'Full occurrence dict does not have city dict')
def test_detail_get_past(self): occ = get(Occurrence, place=F(point=F(geometry=geos.Point(y=0, x=0)))) uri = self.resource().get_resource_uri(occ) resp = self.client.get(uri, data=self.auth_params) resp_dict = try_json_loads(resp.content) self.assertIsNone(resp_dict, 'Returned unexpected existing occurrence') self.assertResponseCode(resp, 410)
def random_points_within_poly(poly, npts): """ Generate n point coordinates that lie within poly NB this can be VERY SLOW if the polygon does not occupy much of its bounding box :return: x, y """ try: # geodjango/OGR interface xmin, ymin, xmax, ymax = poly.extent is_geodjango = True except AttributeError: # shapely interface xmin, ymin, xmax, ymax = poly.bounds is_geodjango = False dx = xmax - xmin dy = ymax - ymin out_idx = np.ones(npts).astype(bool) x = np.zeros(npts) y = np.zeros(npts) while out_idx.sum(): xn = np.random.random(size=out_idx.sum()) * dx + xmin yn = np.random.random(size=out_idx.sum()) * dy + ymin x[out_idx] = xn y[out_idx] = yn if is_geodjango: out_idx = np.array( [not geos.Point(a, b).within(poly) for (a, b) in zip(x, y)]) else: out_idx = np.array([ not geometry.Point(a, b).within(poly) for (a, b) in zip(x, y) ]) return x, y
def can_place_at_position(assettype, meter_x, meter_y): """Returns true if the asset with the given id may be placed at the given position.""" position = geos.Point(float(meter_x), float(meter_y)) placement_areas = assettype.placement_areas # if there is another asset closer to this one than the minimum distance, it may not be placed if assettype.minimum_distance != 0: # TODO: We might want to filter by scenario here as well, otherwise assets from other # scenarios can block this one # TODO: This can be done much more efficiently using a spatial filter and checking if the count is 0 for other_asset in AssetPositions.objects.filter( asset_type_id=assettype).all(): squared_distance = util.get_squared_distance( other_asset.location, position) required_squared_distance = assettype.minimum_distance**2 if squared_distance < required_squared_distance: return not assettype.allow_placement # if there are no placement areas present this asset can be placed according # to it's global setting if not placement_areas: return assettype.allow_placement # check if the position and the placement areas overlap if placement_areas.covers(position): return assettype.allow_placement else: return not assettype.allow_placement
def create_full_study_region_grid(cell_size=1000): sr_extent = StudyRegion.objects.extent() PolygonGrid.objects.all().delete() xmin = sr_extent[0] ymin = sr_extent[1] xmax = sr_extent[2] ymax = sr_extent[3] xdist = xmax - xmin ydist = ymax - ymin xcells = int(xdist / cell_size) + 1 ycells = int(ydist / cell_size) + 1 x_current = xmin y_current = ymin count = 0 for y_num in range(0, ycells): y_current = ymin + (y_num * cell_size) print '.', for x_num in range(0, xcells): x_current = xmin + (x_num * cell_size) corner_pnt = geos.Point(x_current, y_current) create_grid_cell(corner_pnt, cell_size) if create_grid_cell(corner_pnt, cell_size): count += 1 return count
def search(self, x, y, radius=None): """Find the nearest SewerMeasurement. We only use this for the mouse hover function; return the minimal amount of information necessary to show the so-called `lost capacity`. """ pnt = geos.Point(x, y, srid=3857) # aka 900913 qs = ( SewerMeasurement.objects. filter(sewer__sewerage__pk=self.id). filter(the_geom__distance_lte=(pnt, radius)). distance(pnt).order_by('distance') ) try: m = qs[0] # SELECT ... LIMIT 1; except: return [] return [{ 'name': '{:.0%} verloren berging'.format(m.flooded_pct), 'distance': m.distance.m, 'stored_graph_id': m.pk, }]
def showleg(request, lat, lng, contactgiven): print(lat) print(lng) try: user_pt = geos.Point(lng, lat, srid=4326) sd = District.objects.filter(chamber='s').get(geom__intersects=user_pt) hd = District.objects.filter(chamber='h').get(geom__intersects=user_pt) cd = District.objects.filter(chamber='c').get(geom__intersects=user_pt) sw = District.objects.get(chamber='a') houserep = hd.incumbent senrep = sd.incumbent congrep = cd.incumbent ltgov = sw.incumbent string = 'HouseDistrict: %s controlled %s, SenateDistrict %s controlled %s in %s ' % (hd.district, hd.party, sd.district, sd.party, hd.city) return render(request, 'showleg.html', { 'sw': sw, 'cd': cd, 'sd': sd, 'hd': hd, 'congrep': congrep, 'houserep': houserep, 'senrep':senrep, 'ltgov': ltgov, 'lat':lat, 'lng':lng, 'contactgiven':contactgiven}) except Exception as ex: print(ex) return render(request, 'home.html', {'error':True})
def corner_point_array(bbox, mgeom=StudyRegion.objects.multigeometry(), cell_size=1000): # returns an array of points that are within sqrt(2) * cell_size of mgeom threshold = cell_size * np.sqrt(2) simp_mgeom = mgeom.simplify(cell_size / 3, preserve_topology=True).buffer(threshold) xmin = bbox[0] ymin = bbox[1] xmax = bbox[2] ymax = bbox[3] xdist = xmax - xmin ydist = ymax - ymin xcells = int(xdist / cell_size) + 1 ycells = int(ydist / cell_size) + 1 x_current = xmin y_current = ymin points = [] for y_num in range(0, ycells): y_current = ymin + (y_num * cell_size) print '.', for x_num in range(0, xcells): x_current = xmin + (x_num * cell_size) corner_pnt = geos.Point(x_current, y_current) # dist = corner_pnt.distance(mgeom) if corner_pnt.within(simp_mgeom): #dist < threshold: points.append(corner_pnt) return points
def test_adapt_existing_valid(self): occurrence = G(Occurrence, place=F(point=F(geometry=geos.Point(y=0, x=0)))) existing_prices = {G(Price, occurrence=occurrence, quantity=quantity) for quantity in self.quantities} prices = self.adaptor.adapt_m2o(self.event_response, occurrence=occurrence.id) for created, price in prices: self.assertFalse(created, 'Price object created despite existing match') self.assertIn(price, existing_prices, 'Price object returned is not the existing match')
def ripley_correction(centre, radius, poly, n_pt=12): ## NB assuming that the point is INSIDE the domain to begin with pt = geos.Point(centre) if pt.distance(poly.boundary) >= radius: return 1. d = pt.buffer(radius, quadsegs=n_pt).boundary.intersection(poly).length return d / (2 * np.pi * radius)
def test_routing_runtime_error(self): # create expression which will cause error in compilation err_expr = ExpressionFactory.create( _type=self.exp_routing_type, name="runtime error expressin", code='unknown_abc > 1' ) routing_expr = RoutingExpressionFactory.create( _expression=err_expr, _department=self.department, is_active=True, order=1 ) # test signal inside center signal_inside = SignalFactory.create( location__geometrie=geos.Point(4.88, 52.36) ) self.assertIsNone(signal_inside.routing_assignment) # simulate apply routing rules self.dsl_service.process_routing_rules(signal_inside) signal_inside.refresh_from_db() # runtime errors should not effect is_active status routing_expr.refresh_from_db() self.assertTrue(routing_expr.is_active) # 2nd rule should still work self.assertIsNotNone(signal_inside.routing_assignment) self.assertEqual(len(signal_inside.routing_assignment.departments.all()), 1) routing_dep = signal_inside.routing_assignment.departments.first() self.assertEqual(routing_dep.id, self.department.id)
def get_adm_from_coordinates(self, coordinates, parent): point = geos.Point(coordinates['lon'], coordinates['lat'], srid=4326) adm_divisions = AdministrativeDivision.objects.filter(parent=parent) for adm in adm_divisions: adm_geom = geos.fromstr(adm.geom, srid=4326) if point.intersects(adm_geom): return adm
def test_routing_syntax_error(self): # create expression which will cause error in compilation err_expr = ExpressionFactory.create( _type=self.exp_routing_type, name="error expression", code=f'abc location in areas."{self.area._type.name}"."{self.area.code}"' ) routing_expr = RoutingExpressionFactory.create( _expression=err_expr, _department=self.department, is_active=True, order=1 ) # test signal inside center signal_inside = SignalFactory.create( location__geometrie=geos.Point(4.88, 52.36) ) self.assertIsNone(signal_inside.routing_assignment) # simulate apply routing rules self.dsl_service.process_routing_rules(signal_inside) signal_inside.refresh_from_db() # routing rule will be de-activated due to syntax/compilation errors routing_expr.refresh_from_db() self.assertFalse(routing_expr.is_active) # 2nd rule should still work self.assertIsNotNone(signal_inside.routing_assignment) self.assertEqual(len(signal_inside.routing_assignment.departments.all()), 1) routing_dep = signal_inside.routing_assignment.departments.first() self.assertEqual(routing_dep.id, self.department.id)
def __init__(self, *args, **kwargs): # TEST OK # # args puo' essere una stringa del tipo "-45.9876,179.12345" (lat, lng) # args deve avere una coppia di valori numerici (lat e lng) # kwargs deve avere 'lat' e 'lng' come chiavi oppure niente # lat compreso tra -90 e +90 # lng compreso tra -180 e +180 # lat e lng devono essere numeri float # # examples: # LatLng("-45.9876, 179.12345") # LatLng(-45.9876, 179.12345) # LatLng(lat = -45.9876, lng = 179.12345) # self._defaultLat = float(0.0) self._defaultLng = float(0.0) self._lat, self._lng = self._defaultLat, self._defaultLng self._geosPoint = geos.Point((self._lng, self._lat)) if not self._isPositionValue(*args, **kwargs): raise InvalidCoordinateType()