1

In QGIS do I have a EPSG:4326 shapefile that shows the coordinates "-7148300 -3688500" and I need them in the format -64º -31º for further processing in a geodjango app.

However using ogrinfo I get this information from the shapefile:

Feature Count: 22
Extent: (-64.279736, -31.509785) - (-64.092643, -31.332019)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
    DATUM["WGS_1984",
        SPHEROID["WGS_84",6378137,298.257223563]],
    PRIMEM["Greenwich",0],
    UNIT["Degree",0.017453292519943295]]
id: Integer (10.0)

How can I convert the values?

I found useful both solutions:

  1. Getting the coordinates of a given feature id, within Geodjango shell, so I have a reference and work from there using a custom conversion function.

  2. Know the mathematical equivalence between the two pairs of coordinates.

0

4 Answers 4

4

Your QGIS project CRS is set to Google Mercator, which is needed if you want Google or Openstreetmap background by using Openlayers plugin.

The layer CRS can (and should be) different from that, in your case WGS 84 in degrees.

If you want the coordinate display in degrees, but have a Google/OSM background: make a screen copy by File -> Save Picture as, add that picture to the canvas, delete the openlayers layer, Rightclick on the shapefile layer -> Set Layer CRS for project

The picture may look distorted the more away you are from the aequator, but thats what lat/lon coordinates are.

0
2

Another approach: The Geodjango snippet :)

>>> from django.contrib.gis.gdal import SpatialReference, CoordTransform
>>> ct = CoordTransform(SpatialReference(3857), SpatialReference(4326))
>>> pbt = fromstr('POINT(-7146947 -3694804)', srid=3857)
>>> pbt.transform(ct)
>>> print 'x: %s; y: %s; srid: %s' % (pbt.x, pbt.y, pbt.srid)
x: -64.2021172489; y: -31.4766257292; srid: 4326
2

Check out the PROJ.4 command line tool cs2cs. This is certainly not the Geodjango shell solution that you would prefer, but may be best suited for what you are after. Good luck!

0
2

This is the updated solution I have found so far:

First populating the PostGIS database

import os
from django.contrib.gis.utils import LayerMapping
from models import UJ

mj = '/home/martin/data_dir/mapa/tramos.shp'
# Auto-generated `LayerMapping` dictionary for uj model
uj_mapping = {
    'id' : 'id',
    'geom' : 'MULTILINESTRING',
}

def run(verbose=True):
    lm = LayerMapping(UJ, mj, uj_mapping,
                      transform=False, encoding='utf-8')

    lm.save(strict=True, verbose=verbose)

Then Using cs2cs from the comment of @isolier I get the converted coordinates:

cs2cs +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +units=m +k=1.0 +nadgrids=@null +no_defs Then I enter "-7148300 -3688500 " without quotes and get: 64d12'51.377"W 31d25'41.939"S 0.000

Then Using PostGIS

from mapa.models import UJ
from django.contrib.gis.geos import *
from django.contrib.gis.measure import D
pbt = fromstr('POINT(-64.202117 -31.476626)', srid=4326)
qs = UJ.objects.filter(geom__distance_lte = (pbt, D(km=3)))
len(qs)
for uj in qs.distance(pbt): print(uj.id, uj.distance)

I still look for a way to automate this, but I'm closer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.