GDAL has a nice convenient interface to the PROJ4 library.
If you are confident with Python, using the GDAL Python bindings, if you import the osr classes you will have very convenient methods for reading and exporting projection representations to a variety of formats like PROJ4, WKT, Esri .PRJ.
For example this script will convert your .PRJ file of your shapefile to WKT and PROJ4 (the last is used from PostGIS):
#! /usr/bin/env python
import sys
from osgeo import osr
def esriprj2standards(shapeprj_path):
with open(shapeprj_path, 'r') as f:
prj_txt = f.read()
srs = osr.SpatialReference()
srs.ImportFromESRI([prj_txt])
print('Shape prj is: ' + prj_txt)
print('WKT is: ' + str(srs.ExportToWkt()))
print('Proj4 is : ' + str(srs.ExportToProj4()))
srs.AutoIdentifyEPSG()
print('EPSG is: ' + str(srs.GetAuthorityCode(None)))
esriprj2standards(sys.argv[1])
Run this on the command line:
$ python esriprj2standards.py /home/pcorti/data/shapefile/country.prj
Shape prj is: GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
WKT is: GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Proj4 is: +proj=longlat +datum=WGS84 +no_defs
EPSG is: 4326
To sort your geo-lookup by distance, you can do
queries = yourmodel.objects.filter(
location__distance_lte=(base_point, D(m=certain_distance))
).distance(base_point).order_by('distance')
Where location
is the fieldname of the first column in your table, base_point
is an instance of django.contrib.gis.geos.Point
Since it is sorted, to get the closest busstop you then just have to get the first element of queries
as list. I mean
closest_busstop = list(queries)[0]
distance_to_closest_busstop = base_point.distance(closest_busstop)
UPDATE
Perhaps an expert will contradict me (and i would be glad), but it will be necessary to "target" the closest busstop from the returned queryset in one way or another. Given that you have a list of base points, something like this should do the job
django.VERSION < 1.10
get_cbusstop = lambda bpoint: list(
yourmodel.objects.filter(
location__distance_lte=(bpoint, D(m=certain_distance))
).distance(bpoint).order_by('distance')
)[0]
django.VERSION > 1.9
from django.contrib.gis.db.models.functions import Distance
get_cbusstops = lambda bpoint: yourmodel.objects.filter(
location__distance_lte=(bpoint, D(m=certain_distance))
).annotate(
distance=Distance('<busstop_shape_fieldname>', bpoint)
)
get_cbusstop = lambda bpoint: sorted(
get_cbusstops(bpoint), key=lambda obj_:obj_.distance
)[0]
For both version
Then, you can declare a list of base points, say base_points_list
, formed over location_1
,...
,location_n
, and do
closest_busstops = [get_cbusstop(bp) for bp in base_points_list]
Best Answer
Since a GeoQuerySet is a collection of Django model objects, the individual instances should all have the same SRID. The SRID is determined by the SRID of the geometry field in the Django model class for that QuerySet. The default SRID value in Django is 4326.
So to get the SRID of your GeoQuerySet you can get the SRID of any instance in the queryset:
If you are not sure whether some instances in your queryset have been altered in cache, or if you want to change the SRID of the all model instances (i.e. the QuerySet), you can use the GeoQuerySet method transform. The example given in the Django documentation: