Skip to content

Commit

Permalink
[BUGFIX] #67 Print - Scales not well manage
Browse files Browse the repository at this point in the history
The code has to be refactoring to be more robust.
  • Loading branch information
rldhont committed Jul 7, 2014
1 parent 4dbbc70 commit c9dcc02
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 171 deletions.
18 changes: 12 additions & 6 deletions lizmap/install/qgis/montpellier.qgs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<mapcanvas>
<units>degrees</units>
<extent>
<xmin>3.75405748394433703</xmin>
<xmin>3.7453773602525553</xmin>
<ymin>43.52758744508426503</ymin>
<xmax>3.99429656851635029</xmax>
<xmax>4.00297669220813201</xmax>
<ymax>43.69220097167266204</ymax>
</extent>
<projections>1</projections>
Expand All @@ -30,7 +30,7 @@
<layer_coordinate_transform destAuthId="EPSG:4326" srcAuthId="EPSG:3857" srcDatumTransform="-1" destDatumTransform="-1" layerid="frmt_wms_openstreetmap_mapnik_tms20140610162910636"/>
</layer_coordinate_transform_info>
</mapcanvas>
<legend updateDrawingOrder="true" activeLayer="frmt_wms_openstreetmap_mapquest_tms20140610162911446">
<legend updateDrawingOrder="true">
<legendgroup open="true" checked="Qt::Checked" name="Edition">
<legendlayer drawingOrder="1" open="false" checked="Qt::Checked" name="points of interest" showFeatureCount="0">
<filegroup open="false" hidden="false">
Expand Down Expand Up @@ -442,6 +442,12 @@
<prop k="width_border" v="0.26"/>
</layer>
</symbol>
<ComposerScaleBar penColor="#000000" style="Numeric" numMapUnitsPerScaleBarUnit="1" alignment="0" brushColor="#000000" numSegments="4" segmentMillimeters="11.5034" fontColor="#000000" font="Ubuntu,12,-1,5,50,0,0,0,0,0" numUnitsPerSegment="0.01" units="1" labelBarSpace="3" outlineWidth="1" numSegmentsLeft="2" height="3" mapId="0" boxContentSpace="1" unitLabel=" m">
<ComposerItem width="34.092" x="200.908" transparency="0" y="114.833" height="7.76667" positionMode="0" frame="false" outlineWidth="0.3" zValue="7" itemRotation="0" lastValidViewScaleFactor="3.77953" uuid="{e7959b14-6bc8-4be4-aeaf-ea7b23ea37f1}" id="" background="true" blendMode="0" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
</ComposerScaleBar>
<ComposerPicture pictureRotation="0" pictureWidth="30" file="/usr/share/qgis/svg/arrows/Arrow_05.svg" pictureHeight="30" mapId="0">
<ComposerItem width="30" x="5.25781" transparency="0" y="105" height="30" positionMode="0" frame="false" outlineWidth="0.3" zValue="6" itemRotation="0" lastValidViewScaleFactor="2.86256" uuid="{5fc27115-d5a9-44e3-aebe-e6199c6ac0fa}" id="" background="true" blendMode="0" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
Expand All @@ -451,7 +457,7 @@
<ComposerLabel valign="32" labelText="Description" htmlState="0" halign="1" margin="1">
<LabelFont description="Cantarell,10,-1,5,50,0,0,0,0,0"/>
<FontColor red="0" blue="0" green="0"/>
<ComposerItem width="59" x="235" transparency="0" y="0" height="86" positionMode="0" frame="false" outlineWidth="0.3" zValue="5" itemRotation="0" lastValidViewScaleFactor="1.36982" uuid="{06e65314-c767-44f7-a174-12e1ae4baf0d}" id="description" background="true" blendMode="0" positionLock="false">
<ComposerItem width="59" x="235" transparency="0" y="0" height="70.3" positionMode="0" frame="false" outlineWidth="0.3" zValue="5" itemRotation="0" lastValidViewScaleFactor="3.77953" uuid="{06e65314-c767-44f7-a174-12e1ae4baf0d}" id="description" background="true" blendMode="0" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
Expand Down Expand Up @@ -508,7 +514,7 @@
</Annotation>
</Grid>
<AtlasMap fixedScale="0" atlasDriven="0" margin="0.10000000000000001"/>
<ComposerItem width="59" x="235" transparency="0" y="86" height="45" positionMode="0" frame="false" outlineWidth="0.3" zValue="3" itemRotation="0" lastValidViewScaleFactor="1.36982" uuid="{2ebf76e3-6f6b-4c34-95f1-3d642932cfb3}" id="" background="true" blendMode="0" positionLock="false">
<ComposerItem width="59" x="238" transparency="0" y="77.6" height="45" positionMode="0" frame="false" outlineWidth="0.3" zValue="3" itemRotation="0" lastValidViewScaleFactor="0.972936" uuid="{2ebf76e3-6f6b-4c34-95f1-3d642932cfb3}" id="" background="true" blendMode="0" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
Expand Down Expand Up @@ -1542,7 +1548,7 @@
</Annotation>
</Grid>
<AtlasMap fixedScale="0" atlasDriven="0" margin="0.10000000000000001"/>
<ComposerItem width="231.943" x="5.25781" transparency="0" y="18" height="104.6" positionMode="0" frame="false" outlineWidth="0.3" zValue="1" itemRotation="0" lastValidViewScaleFactor="2.86256" uuid="{2835ecbd-6089-4454-b22e-707b558ecef1}" id="" background="true" blendMode="0" positionLock="false">
<ComposerItem width="231.943" x="5.25781" transparency="0" y="18" height="104.6" positionMode="0" frame="false" outlineWidth="0.3" zValue="1" itemRotation="0" lastValidViewScaleFactor="3.77953" uuid="{2835ecbd-6089-4454-b22e-707b558ecef1}" id="" background="true" blendMode="0" positionLock="false">
<FrameColor alpha="255" red="0" blue="0" green="0"/>
<BackgroundColor alpha="255" red="255" blue="255" green="255"/>
</ComposerItem>
Expand Down
213 changes: 48 additions & 165 deletions lizmap/www/js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,6 @@ var lizMap = function() {
else
$('#nominatim-search').remove();

//addComplexPrintControl();
}

function deactivateToolControls( evt ) {
Expand Down Expand Up @@ -1778,25 +1777,44 @@ var lizMap = function() {
return info;
}

function getPrintScale( scale ) {
if (scale >= 9500 && scale <= 950000) {
scale = Math.round(scale / 1000) * 1000;
} else if (scale >= 950000) {
scale = Math.round(scale / 1000000) * 1000000;
} else {
scale = Math.round(scale);
}
return scale;
function getPrintScale( aScales ) {
var scale = map.getScale();
var scaleIdx = aScales.indexOf( scale );
if ( scaleIdx == -1 ) {
var s=0, slen=aScales.length;
while ( scaleIdx == -1 && s<slen ) {
if ( scale > aScales[s] )
scaleIdx = s;
else
s++;
}
if( s == slen ) {
scale = aScales[slen-1];
} else {
scale = aScales[scaleIdx];
}
}
return scale;
}

function drawPrintBox( aLayout, aLayer, aScale ) {
var center = map.getCenter();
var size = aLayout.size;
var units = map.getUnits();
var unitsRatio = OpenLayers.INCHES_PER_UNIT[units];
var w = size.width / 72 / unitsRatio * aScale / 2;
var h = size.height / 72 / unitsRatio * aScale / 2;
var bounds = new OpenLayers.Bounds(center.lon - w, center.lat - h,
center.lon + w, center.lat + h);
var geom = bounds.toGeometry();
var feat = aLayer.features[0];
geom.id = feat.geometry.id;
feat.geometry = geom;
aLayer.drawFeature(feat);
return true;
}

function addPrintControl() {
// if no composers removed print
/*
if (composers.length == 0 ) {
$('#togglePrint').parent().remove();
return false;
}
*/
if ( !config['printTemplates'] || config.printTemplates.length == 0 ) {
$('#togglePrint').parent().remove();
return false;
Expand All @@ -1823,7 +1841,7 @@ var lizMap = function() {

var scaleOptions = '';
for( var i=0, len=scales.length; i<len; i++ ){
var scale = getPrintScale( scales[i] );
var scale = scales[i];
printCapabilities.scales.push(scale);
var scaleText = scale;
if (scale >= 9500 && scale <= 950000) {
Expand Down Expand Up @@ -1930,30 +1948,12 @@ var lizMap = function() {
deactivateToolControls(evt);

var layout = this.layout;
var units = map.getUnits();
// get scale and update the select
var res = map.getResolution()/2;
var scale = OpenLayers.Util.getScaleFromResolution(res, units);
scale = getPrintScale( scale );
var scaleIdx = printCapabilities.scales.indexOf( scale );
if ( scaleIdx == -1 ) {
res = map.getResolution();
scale = OpenLayers.Util.getScaleFromResolution(res, units);
scale = getPrintScale( scale );
}
// get print scale
var scale = getPrintScale( printCapabilities.scales );
// update the select
$('#print-menu select.btn-print-scales').val(scale);

var center = map.getCenter();
var size = layout.size;
var unitsRatio = OpenLayers.INCHES_PER_UNIT[units];
var w = size.width / 72 / unitsRatio * scale / 2;
var h = size.height / 72 / unitsRatio * scale / 2;
var bounds = new OpenLayers.Bounds(center.lon - w, center.lat - h,
center.lon + w, center.lat + h);
var geom = bounds.toGeometry();
var feat = layer.features[0];
geom.id = feat.geometry.id;
feat.geometry = geom;
// draw print box
drawPrintBox( layout, layer, scale );

$('#togglePrint').parent().addClass('active');
$('#print-menu .title .text').html(layout.name);
Expand Down Expand Up @@ -2020,20 +2020,9 @@ var lizMap = function() {
$('#print-menu select.btn-print-scales').change(function() {
if ( dragCtrl.active && layer.getVisibility() ) {
var self = $(this);
var units = map.getUnits();
var scale = parseFloat(self.val());
var center = map.getCenter();
var size = dragCtrl.layout.size;
var unitsRatio = OpenLayers.INCHES_PER_UNIT[units];
var w = size.width / 72 / unitsRatio * scale / 2;
var h = size.height / 72 / unitsRatio * scale / 2;
var bounds = new OpenLayers.Bounds(center.lon - w, center.lat - h,
center.lon + w, center.lat + h);
var geom = bounds.toGeometry();
var feat = layer.features[0];
geom.id = feat.geometry.id;
feat.geometry = geom;
layer.drawFeature(feat);
// draw print box
drawPrintBox( dragCtrl.layout, layer, scale );
}
});
$('#print-menu button.btn-print-launch').click(function() {
Expand Down Expand Up @@ -2084,121 +2073,15 @@ var lizMap = function() {
map.events.on({
"zoomend": function() {
if ( dragCtrl.active && layer.getVisibility() ) {
var units = map.getUnits();
var res = map.getResolution()/2;
var scale = OpenLayers.Util.getScaleFromResolution(res, units);
scale = getPrintScale( scale );
var scaleIdx = printCapabilities.scales.indexOf( scale );
if ( scaleIdx == -1 ) {
res = map.getResolution();
scale = OpenLayers.Util.getScaleFromResolution(res, units);
scale = getPrintScale( scale );
}
// get scale
var scale = getPrintScale( printCapabilities.scales );
// update the select
$('#print-menu select.btn-print-scales').val(scale);
var center = map.getCenter();
var size = printCapabilities.layouts[0].size;
var unitsRatio = OpenLayers.INCHES_PER_UNIT[units];
var w = size.width / 72 / unitsRatio * scale / 2;
var h = size.height / 72 / unitsRatio * scale / 2;
var bounds = new OpenLayers.Bounds(center.lon - w, center.lat - h,
center.lon + w, center.lat + h);
var geom = bounds.toGeometry();
var feat = layer.features[0];
geom.id = feat.geometry.id;
feat.geometry = geom;
layer.drawFeature(feat);
}
}
});
}

function addComplexPrintControl() {
var ptTomm = 0.35277; //conversion pt to mm
var printCapabilities = {scales:[],layouts:[]};
for (var i=0, len=composers.length; i<len; i++) {
var composer = composers[i];
var composerMap = composer.getElementsByTagName('ComposerMap');
if (composerMap.length != 0) {
composerMap = composerMap[0];
var mapWidth = Number(composer.getElementsByTagName('ComposerMap')[0].getAttribute('width')) / ptTomm;
var mapHeight = Number(composer.getElementsByTagName('ComposerMap')[0].getAttribute('height')) / ptTomm;
//for some strange reason we need to provide a "map" and a "size" object with identical content
printCapabilities.layouts.push({
"name": composer.getAttribute('name'),
"map": {
"width": mapWidth,
"height": mapHeight
},
"size": {
"width": mapWidth,
"height": mapHeight
},
"rotation": true
});
}
}
var layer = map.getLayersByName('Print');
if ( layer.length == 0 ) {
layer = new OpenLayers.Layer.Vector('Print');
map.addLayer(layer);
layer.setVisibility(false);
} else
layer = layer[0];
if ( layer.features.length == 0 )
layer.addFeatures([
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(-1, -1),
new OpenLayers.Geometry.Point(1, -1),
new OpenLayers.Geometry.Point(1, 1),
new OpenLayers.Geometry.Point(-1, 1)
])
])
)
]);
var transformCtrl = new OpenLayers.Control.TransformFeature(layer,{
preserveAspectRatio: true,
rotate: true,
geometryTypes: ['OpenLayers.Geometry.Polygon'],
eventListeners: {
"activate": function(e) {
var units = map.getUnits();
var res = map.getResolution()/2;
var scale = OpenLayers.Util.getScaleFromResolution(res, units);
var center = map.getCenter();
var size = printCapabilities.layouts[0].size;
var unitsRatio = OpenLayers.INCHES_PER_UNIT[units];
var w = size.width / 72 / unitsRatio * scale / 2;
var h = size.height / 72 / unitsRatio * scale / 2;
var bounds = new OpenLayers.Bounds(center.lon - w, center.lat - h,
center.lon + w, center.lat + h);
var geom = bounds.toGeometry();
var feat = layer.features[0];
geom.id = feat.geometry.id;
feat.geometry = geom;
layer.setVisibility(true);
//e.object.setFeature(feat);
},
"deactivate": function(e) {
//layer.destroyFeatures();
layer.setVisibility(false);
},
"beforesetfeature": function(e) {
},
"setfeature": function(e) {
},
"beforetransform": function(e) {
},
"transformcomplete": function(e) {
// draw print box
drawPrintBox( dragCtrl.layout, layer, scale );
}
}
});
map.addControls([transformCtrl]);
controls['printTransform'] = transformCtrl;
//pour activer il suffit de faire un setFeature
//transformCtrl.setFeature(layer.features[0]);
return true;
}

function addEditionControls() {
Expand Down

0 comments on commit c9dcc02

Please sign in to comment.