convert lat / lon to pixel coordinates

2,415 views
Skip to first unread message

tm...@s.netic.de

unread,
Sep 25, 2013, 4:18:45 PM9/25/13
to map...@googlegroups.com
Hello,

all the map drawing using mapnik works fine, thanks for your hints.

For some special handling i'd like to get some more background on the conversion of coordinates, it would be great if you had some hint:

If i have lat /lon as coordinate, how can i transform that to pixel coordinates?  I tried Projection.forward(Coord(lat,lon)) but that results in enormously high values that are out of pixel coordinate space.

Also, if i have pixel coordinates, how can i convert those back to lat/lon?


Best regards
Torsten

Martin Koppenhoefer

unread,
Sep 25, 2013, 4:53:39 PM9/25/13
to map...@googlegroups.com



2013/9/25 <tm...@s.netic.de>
it depends on the zoom level. In zoom 0 one tile (normally) is 256x256pixels and covers from -180 to 180 longitude and -85.0511 to 85.0511 latitude. For every zoom level the width and length is doubled.

A shortcut can be found here: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

cheers,
Martin

tm...@s.netic.de

unread,
Sep 27, 2013, 3:55:57 AM9/27/13
to map...@googlegroups.com
Hello,

thanks, but that was not what exactly what i had in mind.  The tiles on openstreetmap are rendered in a fixed scale.

But when i use mapnik myself in own python scripts and also i set the projection myself.  For example i draw a map
of 1600 x 800, the bounding box i give is (12.245882, 44.614474, 14.387365, 45.475679), the projection is mercator, how do i find out where in the image (of dimension 1600 x 800) the point 12.7, 45.2 is?

And reverse, in the image there is pixel 300, 200, how do i know which lat / lon that maps to?


Best regards
Torsten

tm...@s.netic.de

unread,
Sep 30, 2013, 5:24:32 PM9/30/13
to map...@googlegroups.com
Hello,

i'd like to draw a GPX track into the map and i'd like to do this on my own as i'd like to color-code
the speed into the track and additionally draw some more information.

I wonder if somebody has the same use case?


Thanks for any hints
Torsten

Yggy King

unread,
Jan 6, 2015, 1:21:03 AM1/6/15
to map...@googlegroups.com
I'll bump this old thread ... I recently discovered mapnik and it looks like just the thing for a project I am working on. I want to be able to combine GPX track files with hi-res satellite imagery. I am using ECW images from the Vancouver 2011 Orthophotos public data set, which I want to combine with GPX tracks gathered while running with a GPS phone or watch.

I have been able to use mapnik to render either images or tracks, but the magic of projection systems still eludes me and I haven't yet figured out how to overlay one on the other. The GPX data is a lat/long point set, while the ECW images (or equivalent geotiff or TFW file) have coordinates defined by a World file, which are (typically) based on meters. 

This seems like a simple use case, but I have read through all the mapnik tutorials and google extensively without finding any clear and simple example. I would very much appreciate if someone can point me in the right direction. Should I be modifying the TFW file to convert pixels to lat/long instead of meters? Or setting the projection parameters for the raster layer and the track layer?

As an example of the sort of thing I'm trying to do, here is an image I rendered from Google Earth.
I want have much more control over line styling and image size, for which mapnik seems ideally suited.


Many thanks!

Andy Allan

unread,
Jan 7, 2015, 6:43:35 AM1/7/15
to mapnik
On 6 January 2015 at 06:21, Yggy King <yggy...@gmail.com> wrote:
> Should I be modifying the TFW file to convert pixels to lat/long instead of meters?

No.

> Or setting the projection parameters for the raster layer and the track layer?

Yes. You should have three projections in total:

* The projection you want the output to be, set as the 'srs' attribute
of the root Map element
* The projection of the ECW/geotiff image, set as the 'srs' attribute
of the raster Layer element
* The projection of the GPX files, set as the 'srs' attribute of the
gpx Layer element.

For example, if the GPX layer is in latlon your srs will be
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

Make sure the layer definitions' projections match what they really
are in the files, and you should find it all works!

Thanks,
Andy

P.S. for getting started with multiple layers and projections, I can
recommend using Tilemill https://www.mapbox.com/tilemill/

Yggy King

unread,
Jan 8, 2015, 12:08:48 AM1/8/15
to map...@googlegroups.com
Great, many thanks Andy, that got me pointed in the right direction.

The orthophoto data is NAD83, the GPX data is WGS84, and spatialreference.org provides the corresponding mapnik projections.

I also realized I had to adjust the scale factors in the TFW file because I had resized the image -- 184,000 x 115,000 was a little unmanageable!!

The following script is now getting me correct results, huzzah!

#!/usr/bin/env python

import mapnik

# City of Vancouver orthophotos are NAD83:
projNAD83 = '+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83 +units=m +no_defs'

# GPX files are EPSG:4326, i.e. WGS84:
projWGS84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'

map = mapnik.Map(1024, 512, projNAD83)
map.background = mapnik.Color(0, 0, 0, 0)
map.aspect_fix_mode = mapnik.aspect_fix_mode.GROW_BBOX

style = mapnik.Style()
rule = mapnik.Rule()

rasterSymbolizer = mapnik.RasterSymbolizer()
rule.symbols.append(rasterSymbolizer)

polygonSymbolizer = mapnik.PolygonSymbolizer()
polygonSymbolizer.fill_opacity = 0.0
rule.symbols.append(polygonSymbolizer)

lineSymbolizer = mapnik.LineSymbolizer(mapnik.Color('red'), 1.0)
rule.symbols.append(lineSymbolizer)

style.rules.append(rule)
map.append_style('My Style', style)

# City map raster layer from geotiff.
lyrCity = mapnik.Layer('City', projNAD83)
# Gdal uses the associated .tfw file containing projection/extent information.
# Note that the scale factors in the .tfw need to be updated if the image has been resized.
lyrCity.datasource = mapnik.Gdal(file='city_vancouver_utm10_2011.tif')
lyrCity.styles.append('My Style')

# GPS track from GPX file.
lyrTrack = mapnik.Layer('Track', projWGS84)
lyrTrack.datasource = mapnik.Ogr(file = 'riley-park-perimeter-run.gpx', layer = 'tracks')
lyrTrack.styles.append('My Style')

map.layers.append(lyrCity)
map.layers.append(lyrTrack)

map.zoom_all()

mapnik.render_to_file(map, 'test.png', 'png')



On Wednesday, 7 January 2015 03:43:35 UTC-8, gravitystorm wrote:
On 6 January 2015 at 06:21, Yggy King <yggy...@gmail.com> wrote:
> Should I be modifying the TFW file to convert pixels to lat/long instead of meters?

No.

> Or setting the projection parameters for the raster layer and the track layer?

Yes. You should have three projections in total:
<snip>

valo...@gmail.com

unread,
Apr 5, 2017, 8:02:21 AM4/5/17
to mapnik
Hello To Mo. I saw and read your writings. i am working now to this problem, how can i convert pixels->latlon and latlon->pixels. I saw 4years passed. Do you work in this problem yet? and Have you found any solution? you can also contact me via mail valo...@gmail.com

четверг, 26 сентября 2013 г., 0:18:45 UTC+4 пользователь To Mo написал:

Kevin Kenny

unread,
Apr 5, 2017, 3:37:28 PM4/5/17
to map...@googlegroups.com
After you've created and populated your map, and done any pan and zoom operations to establish what its bounding box is, then you will have a 'view transformation' available. The 'Map.view_transform()' method will return it. (The transformation will be invalid, of course, if you do any further panning and zooming.) The returned object is an instance of ViewTransform.

The 'ViewTransform.forward' and 'ViewTransform.backward' methods, which can apply to either Coord or Box2d objects, will map from geographic coordinates to pixels and pixels to geographic coordinates, respectively.

I don't recall without source-diving (and I don't have time to go source-diving right now) whether the geographic coordinates are longitude/latitude using the spatial reference system of the map (which would make sense), or whether they're 'false easting' and 'false northing'. If the latter, then Projection.forward and Projection.backward would finish the task.

Reply all
Reply to author
Forward
0 new messages