Skip to content

Commit

Permalink
Update examples to use projection default options
Browse files Browse the repository at this point in the history
This also adds a graticule control to the polar-projections example.
  • Loading branch information
ahocevar committed Oct 16, 2014
1 parent 1d5cd8e commit 08d8c5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 66 deletions.
15 changes: 8 additions & 7 deletions examples/graticule.html
Expand Up @@ -26,7 +26,12 @@
<script src="http://svn.osgeo.org/metacrs/proj4js/trunk/lib/proj4js-compressed.js"></script>
<script type="text/javascript">
Proj4js.defs["EPSG:42304"]="+title=Atlas of Canada, LCC +proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs";

OpenLayers.Projection.defaults["EPSG:42304"] = {
maxExtent: [-2200000,-712631,3072800,3840000],
worldExtent: [-180, 30, 180, 90],
units: 'm'
}
OpenLayers.Projection.defaults["EPSG:4326"].worldExtent = [-1800, -90, 1800, 90];
var graticuleCtl1, graticuleCtl2;
var map1, map2;
function init(){
Expand Down Expand Up @@ -56,7 +61,6 @@
};

function initProjected(){
var extent = new OpenLayers.Bounds(-2200000,-712631,3072800,3840000);
graticuleCtl2 = new OpenLayers.Control.Graticule({
labelled: true,
targetSize: 200
Expand All @@ -68,10 +72,6 @@
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Navigation()
],
//scales: tempScales,
maxExtent: extent,
maxResolution: 50000,
units: 'm',
projection: 'EPSG:42304'
};
map2 = new OpenLayers.Map('map2', mapOptions);
Expand All @@ -85,7 +85,7 @@
});

map2.addLayers([dm_wms]);
if (!map2.getCenter()) map2.zoomToExtent(extent);
map2.setCenter([436400, 1564045], 1);
}
</script>
</head>
Expand Down Expand Up @@ -114,3 +114,4 @@ <h1 id="title">Graticule Example</h1>
</ul>
</body>
</html>

106 changes: 47 additions & 59 deletions examples/polar-projections.js
@@ -1,81 +1,69 @@
var map, layer, overlay;

var projectionOptions = {
'EPSG:3574': {
projection: new OpenLayers.Projection('EPSG:3574'),
units: 'm',
maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054),
maxResolution: 5505054 / 128,
numZoomLevels: 18
},
'EPSG:3576': {
projection: new OpenLayers.Projection('EPSG:3576'),
units: 'm',
maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054),
maxResolution: 5505054 / 128,
numZoomLevels: 18
},
'EPSG:3571': {
projection: new OpenLayers.Projection('EPSG:3571'),
units: 'm',
maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054),
maxResolution: 5505054 / 128,
numZoomLevels: 18
},
'EPSG:3573': {
projection: new OpenLayers.Projection('EPSG:3573'),
units: 'm',
maxExtent: new OpenLayers.Bounds(-5505054, -5505054, 5505054, 5505054),
maxResolution: 5505054 / 128,
numZoomLevels: 18
}
OpenLayers.Projection.defaults['EPSG:3574'] = {
maxExtent: [-5505054, -5505054, 5505054, 5505054],
worldExtent: [-180.0, 0.0, 180.0, 90.0],
units: 'm'
};
OpenLayers.Projection.defaults['EPSG:3576'] = {
maxExtent: [-5505054, -5505054, 5505054, 5505054],
worldExtent: [-180.0, 0.0, 180.0, 90.0],
units: 'm'
};
OpenLayers.Projection.defaults['EPSG:3571'] = {
maxExtent: [-5505054, -5505054, 5505054, 5505054],
worldExtent: [-180.0, 0.0, 180.0, 90.0],
units: 'm'
};
OpenLayers.Projection.defaults['EPSG:3573'] = {
maxExtent: [-5505054, -5505054, 5505054, 5505054],
worldExtent: [-180.0, 0.0, 180.0, 90.0],
units: 'm'
};

function setProjection() {
projCode = this.innerHTML;
var oldExtent = map.getExtent();
var oldCenter = map.getCenter();
var oldProjection = map.getProjectionObject();
// map projection is controlled by the base layer
map.baseLayer.addOptions(projectionOptions[projCode]);

// the base layer controls the map projection
layer.addOptions({projection: projCode});

// with the base layer updated, the map has the new projection now
var newProjection = map.getProjectionObject();

// transform the center of the old projection, not the extent
map.setCenter(
oldCenter.transform(oldProjection, newProjection,
map.getZoomForExtent(oldExtent.transform(oldProjection, newProjection))
));

for (var i=map.layers.length-1; i>=0; --i) {
// update grid settings
map.layers[i].addOptions(projectionOptions[projCode]);
// redraw layer - just in case center and zoom are the same in old and
// new projection
map.layers[i].redraw();
}
map.setCenter(oldCenter.transform(oldProjection, newProjection));

// update overlay layers here
overlay.addOptions({projection: newProjection});

// re-fetch images for all layers
layer.redraw();
overlay.redraw();
}

function init() {
map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(
'world',
'http://v2.suite.opengeo.org/geoserver/wms',
{layers: 'world', version: '1.1.1'},
projectionOptions['EPSG:3574']
'countries',
'http://suite.opengeo.org/geoserver/wms',
{layers: 'opengeo:borders', version: '1.1.1'},
{projection: 'EPSG:3574', displayOutsideMaxExtent: true}
);
overlay = new OpenLayers.Layer.WMS(
'world',
'http://v2.suite.opengeo.org/geoserver/wms',
{transparent: 'true', layers: 'world:borders', styles: 'line'},
projectionOptions['EPSG:3574']
'cities',
'http://suite.opengeo.org/geoserver/wms',
{layers: 'opengeo:cities', version: '1.1.1', transparent: true},
{projection: 'EPSG:3574', displayOutsideMaxExtent: true,
isBaseLayer: false}
);
overlay.isBaseLayer = false;
map.addLayers([layer, overlay]);
map.zoomToMaxExtent();

map = new OpenLayers.Map('map', {
center: [25000, 25000],
zoom: 1,
layers: [layer, overlay]
});
map.addControl(new OpenLayers.Control.Graticule());

// add behaviour to dom elements
document.getElementById('epsg3574').onclick = setProjection;
document.getElementById('epsg3576').onclick = setProjection;
Expand Down

0 comments on commit 08d8c5a

Please sign in to comment.