def get_lon_lat(self, lonlat):
"Unpacks longitude, latitude from GEOS Points and 2-tuples."
if isinstance(lonlat, Point):
lon, lat = lonlat.coords
else:
lon, lat = lonlat
return lon, lat
python类Point()的实例源码
def get_width_height(self, extent):
"""
Returns the width and height for the given extent.
"""
# Getting the lower-left, upper-left, and upper-right
# coordinates from the extent.
ll = Point(extent[:2])
ul = Point(extent[0], extent[3])
ur = Point(extent[2:])
# Calculating the width and height.
height = ll.distance(ul)
width = ul.distance(ur)
return width, height
def geos(self, query):
"Return a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def geos(self, query):
"Returns a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def _handle_empty_point(self, geom):
from django.contrib.gis.geos import Point
if isinstance(geom, Point) and geom.empty:
if self.srid:
# PostGIS uses POINT(NaN NaN) for WKB representation of empty
# points. Use it for EWKB as it's a PostGIS specific format.
# https://trac.osgeo.org/postgis/ticket/3181
geom = Point(float('NaN'), float('NaN'), srid=geom.srid)
else:
raise ValueError('Empty point is not representable in WKB.')
return geom
def geos(self, query):
"Return a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def geos(self, query):
"Returns a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def _handle_empty_point(self, geom):
from django.contrib.gis.geos import Point
if isinstance(geom, Point) and geom.empty:
if self.srid:
# PostGIS uses POINT(NaN NaN) for WKB representation of empty
# points. Use it for EWKB as it's a PostGIS specific format.
# https://trac.osgeo.org/postgis/ticket/3181
geom = Point(float('NaN'), float('NaN'), srid=geom.srid)
else:
raise ValueError('Empty point is not representable in WKB.')
return geom
def geos(self, query):
"Return a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def post(self, request, data):
if not data.get('name') and not data.get('lat'):
return HttpResponseServerError("Name or coords is required.")
if data.get('name'):
model = {'city': City, 'subregion': Subregion, 'region': Region}.get(data.get('model'))
if not model:
return HttpResponseServerError("Invalid model: {}".format(data.get('model')))
obj = get_object_or_404(model, pk=data['name'])
else:
# http://stackoverflow.com/a/35079313
pnt = Point(float(data.get('lon', 0)), float(data.get('lat', 0)), srid=4326)
order_by_expression = CombinedExpression(F('location'), '<->', GeomValue(pnt))
try:
obj = City.objects.order_by(order_by_expression)[0]
except IndexError:
return
fields = ['subregion', 'region', 'region__country', 'country']
data = {getattr(obj, key).__class__.__name__: {x: getattr(getattr(obj, key), x) for x in ['id', 'name']}
for key in fields if hasattr(obj, key)}
if not data.get('name'):
# Is geo coord search
data['city'] = {'id': obj.pk, 'name': obj.name}
if hasattr(obj, 'location'):
order_by_expression = CombinedExpression(F('location'), '<->', GeomValue(obj.location))
try:
data['postal_code'] = PostalCode.objects.order_by(order_by_expression)[0].code
except IndexError:
pass
data['coords'] = obj.location.coords
return data
def post(self, request):
if not (request.POST.get('uuid') and
request.POST.get('long') and
request.POST.get('lat') and
request.POST.get('bands')
):
return Response("Missing arguments", status=status.HTTP_406_NOT_ACCEPTABLE)
req_bands = request.POST.get('bands').split(',')
for band in req_bands:
if not (band in BTS.bands):
return Response("Invalid Arguments", status=status.HTTP_400_BAD_REQUEST)
pnt = GEOSGeometry(Point(float(request.POST.get('long')),
float(request.POST.get('lat'))))
with transaction.atomic():
tower = BTS.objects.get(uuid=request.POST.get('uuid'))
nearby_towers = BTS.objects.filter(
location__distance_lt=(pnt,D(km=RANGE))).exclude(uuid=request.POST.get('uuid'))
used_channels = dict.fromkeys(BTS.bands.keys(), set())
for tower in nearby_towers:
if (tower.band): #skip those without set bands
used_channels[tower.band].add(tower.channel)
free_channels = dict.fromkeys(req_bands)
for band in req_bands:
free_channels[band] = BTS.bands[band]['valid_values'].difference(used_channels[band])
if (len(free_channels[band]) > 0): #something available
return Response({ band : free_channels[band],
'power_level' : POWER_LEVEL},
status=status.HTTP_200_OK)
return Response("No Available Bands", status=status.HTTP_404_NOT_FOUND)
def post(self, request):
if not (
request.POST.get('uuid') and
request.POST.get('lat') and
request.POST.get('long') and
request.POST.get('band') and
request.POST.get('channel') and
request.POST.get('power_level')
):
return Response("Missing Arguments", status=status.HTTP_406_NOT_ACCEPTABLE)
if not (
request.POST.get('band') in BTS.bands and
request.POST.get('channel').isdigit() and
request.POST.get('power_level').isdigit()
):
return Response("Invalid Arguments", status=status.HTTP_400_BAD_REQUEST)
pnt = GEOSGeometry(Point(float(request.POST.get('long')),
float(request.POST.get('lat'))))
with transaction.atomic():
tower = BTS.objects.get(uuid=request.POST.get('uuid'))
nearby_towers = BTS.objects.filter(
location__distance_lt=(pnt,D(km=RANGE))).filter(
band=request.POST.get('band')).exclude(uuid=request.POST.get('uuid'))
for t in nearby_towers:
if (int(request.POST.get('channel')) == t.channel):
return Response("Channel In Use", status=status.HTTP_409_CONFLICT)
#no one interfered
tower.channel = int(request.POST.get('channel'))
tower.location = pnt
tower.band = request.POST.get('band')
tower.save()
return Response("Success", status=status.HTTP_200_OK)
def get_point(self, srid=4326):
from django.contrib.gis.geos import Point
return Point(float(self._lng), float(self._lat), srid=srid)
def add_potential_georeference(self, lat, long, profile_name, locality_name=False, precision_m=False):
# Geographical position
point = Point(long, lat)
gp, created = GeographicalPosition.objects.get_or_create(point=point)
# Precision
if precision_m:
gp.precision_m = precision_m
# Author profile
profile = Profile.objects.get(name=profile_name)
# Locality name
if not locality_name:
ln = self.locality_name
else:
ln, created = LocalityName.objects.get_or_create(locality_name=locality_name)
# Georeference
gr, created = GeoReference.objects.get_or_create(geographical_position=gp,
locality_name=ln,
author=profile)
# Add it to list
if gr not in self.potential_georeferences.values():
self.potential_georeferences.add(gr)
def geos(self, query):
"Returns a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def get_lon_lat(self, lonlat):
"Unpacks longitude, latitude from GEOS Points and 2-tuples."
if isinstance(lonlat, Point):
lon, lat = lonlat.coords
else:
lon, lat = lonlat
return lon, lat
def get_width_height(self, extent):
"""
Returns the width and height for the given extent.
"""
# Getting the lower-left, upper-left, and upper-right
# coordinates from the extent.
ll = Point(extent[:2])
ul = Point(extent[0], extent[3])
ur = Point(extent[2:])
# Calculating the width and height.
height = ll.distance(ul)
width = ul.distance(ur)
return width, height
def geos(self, query):
"Return a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####
def geos(self, query):
"Returns a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
from django.contrib.gis.geos import Point
return Point(ll, srid=4326)
else:
return None
# #### GeoIP Database Information Routines ####