Flutterby™! : Projection struggles

Next unread comment / Catchup all unread comments User Account Info | Logout | XML/Pilot/etc versions | Long version (with comments) | Weblog archives | Site Map | | Browse Topics

Projection struggles

2010-12-28 19:07:30.64234+00 by Dan Lyke 5 comments

Java has hurt me in the past, but it seemed like Mapnik's WMS server would require a bunch of hacking to do what I wanted, allow people to do dynamic GeoSQL queries from web pages, and I'd heard great things about GeoServer, and I said to myself "I know that Java usually indicates that people don't know how to make comprehensible software, but this time could be different", and downloaded GeoServer.

After much struggle, I got it running (Actually, that's not true, it ran out of the box on my laptop, but was painful to get running on my server. Who knows why? It's Java, after all...). And I imported my .shp file, which appears to be in EPSG:2226, and wrote up a little OpenLayers JavaScript code, and... my lines didn't match up with Google Maps layers. Worse, they crawled around with my zoom, if I got really close they were within a block or so, if I zoomed back out they were miles off.

So I tried using ogr2ogr to reproject into EPSG:900913 and EPSG:4326, re-imported, and I'm getting what looks like the same results.

And my experiments to try to mix data from OpenStreetMap always end up with me looking somewhere off the west coast of Africa. Which I assume is due to OSM using EPSG:900913 rather than "spherical mercator".

And what the hell is the difference between EPSG:900913 and EPSG:3857?

Anyway, for Christmas this year I want a pony and a baby doll and the procedure to use OpenLayers to show Bing, OSM and Google base layers, and my own layers that start out as these shape files, preferably from a PostGIS query because then I can do interesting stuff with the data.

[ related topics: Interactive Drama Software Engineering History California Culture Sports Maps and Mapping ]

comments in descending chronological order (reverse):

#Comment Re: made: 2010-12-30 19:50:19.318645+00 by: Dan Lyke

Hopefully it'll help some future web surfer to know that the solution was to specify the projection in the OpenLayers map instantiation:

var options = {
        projection: new OpenLayers.Projection("EPSG:900913"),
        displayProjection: new OpenLayers.Projection("EPSG:4326"),
        units: "m",
        numZoomLevels: 20,
        maxResolution: 156543.0339,
        maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
        20037508, 20037508.34)
        map = new OpenLayers.Map('map', options);

#Comment Re: made: 2010-12-29 07:10:01.267728+00 by: ebradway [edit history]

For more than you really want to know about Web Mercator, check out this presentation from GIS In the Rockies this past September. Noel Zinn, a real-life geodeticist took apart this pseudo-Mercator projection. In summary, Zinn says that Web Mercator is great because it's computationally inexpensive. But it is inadequate for most survey- grade efforts.

Somewhere I saw a reference where a Googler stated that pseudo-Mercator was chosen to ensure that streets that met at right angles appeared as right angles in northern latitudes. Mercator's projections were famous for allowing straight lines of bearing to be true (i.e., it's an azmuthal projection).

As for the EPSG code... EPSG codes are defined for 0-32767. Anything greater is considered proprietary. This includes Google's 900913 and ESRI's 102113 (or 102110). EPSG assigned 3785 but later decided 3857 was a better place for it. Don't ask me why all the confusion... <tinfoilhat>It's OGC/EPSG trying to stick it to Google for creating a de facto standard without asking for their input.</tinfoilhat>... All four codes refer to mathematically isomorphic projections (i.e., they produce the same results). The OpenLayers Wiki also has a nice description.

If OSM is landing you off the coast of Africa, then for some reason you are in the wrong coordinate system. Sounds like your shapefile is in geographic coordinates (decimal degrees, lat/long). Web Mercator uses meters northing/easting from the same origin as GCS.

Two suggestions:

  1. Upload your shapefile to GeoCommons and mess with it there until it displays where you expect it.
  2. Send me the file and let me take a look at it. I'm probably going to be in OpenLayers-land tomorrow anyhow.

Related, loosely - but mostly I want to anchor this link to this text.

#Comment Re: made: 2010-12-29 00:24:49.841184+00 by: Dan Lyke

Yeah, there's a lot to be said for "convert everything to KML and let Google sort it out"...

#Comment Re: made: 2010-12-28 21:54:55.892175+00 by: meuon

These are why my playing with real GIS stuff has kept me to putting things on top of Google maps. At least, until these issues have a real budget.

#Comment Re: made: 2010-12-28 19:40:41.590648+00 by: Dan Lyke

Okay, I can get the OSM and Google Maps layers to line up if I set up the Google Maps with "sphericalMercator": true in the options and set up all of the layers projections with:

aliasproj = new OpenLayers.Projection("EPSG:3857");
    osm.projection = aliasproj;
    gmap.projection = aliasproj;

but I'm having absolutely no luck figuring out how to get a layer out of GeoServer that'll register to those.