Set Composer scale with pyqgis

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Set Composer scale with pyqgis

roy roy
Hi, I'm trying to print a map using pyqgis and an existing template:

# i already loaded a layer and zoomed to the layer:

qgis.utils.iface.zoomToActiveLayer()

# now i need to zoom to a "precise" scale
canvas = qgis.utils.iface.mapCanvas()
canvas.zoomScale(2500) # this works for the canvas not in the composer

# load the template
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
composition.loadFromTemplate(document)
map_item = composition.getComposerItemById('map')
map_item.setMapCanvas(canvas)

# at this point i need to set the extent to fit the layer
map_item.zoomToExtent(canvas.extent()) # this will not be in scale 2500
of course

now i need the scale to be say 2500 but i cannot find how to
set the value

thanks for any help, Roy

_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Set Composer scale with pyqgis

boesiii
Try:

map_item.setNewScale(canvas.scale())
Reply | Threaded
Open this post in threaded view
|

Re: Set Composer scale with pyqgis

roy roy
In reply to this post by roy roy
Hi,

still the only way i found to set the composer
map is using

zoomToExtent(canvas.extent())

but not able to set a custom defined scale,
any hint to find some documentation
or some way to set the composer scale using pyQGIS?
thanks again

Il 07/07/2016 12:59, roy roy ha scritto:

> Hi, I'm trying to print a map using pyqgis and an existing template:
>
> # i already loaded a layer and zoomed to the layer:
>
> qgis.utils.iface.zoomToActiveLayer()
>
> # now i need to zoom to a "precise" scale
> canvas = qgis.utils.iface.mapCanvas()
> canvas.zoomScale(2500) # this works for the canvas not in the composer
>
> # load the template
> template_file = file(template_path)
> template_content = template_file.read()
> template_file.close()
> document = QDomDocument()
> document.setContent(template_content)
> composition = QgsComposition(canvas.mapSettings())
> composition.loadFromTemplate(document)
> map_item = composition.getComposerItemById('map')
> map_item.setMapCanvas(canvas)
>
> # at this point i need to set the extent to fit the layer
> map_item.zoomToExtent(canvas.extent()) # this will not be in scale 2500
> of course
>
> now i need the scale to be say 2500 but i cannot find how to
> set the value
>
> thanks for any help, Roy
>
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer

_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Set Composer scale with pyqgis

roy roy
Hi,

for some reason my email client did not download
or received the answer to my question, sent by "boesiii",
that i find righ now browsing the mailing list archive.

So, using

map_item.setNewScale(canvas.scale())


i can surely set the scale for the map composer

thank you Boesiii

and sorry to the list for my second
message.


Il 11/07/2016 09:55, roy roy ha scritto:

> Hi,
>
> still the only way i found to set the composer
> map is using
>
> zoomToExtent(canvas.extent())
>
> but not able to set a custom defined scale,
> any hint to find some documentation
> or some way to set the composer scale using pyQGIS?
> thanks again
>
> Il 07/07/2016 12:59, roy roy ha scritto:
>> Hi, I'm trying to print a map using pyqgis and an existing template:
>>
>> # i already loaded a layer and zoomed to the layer:
>>
>> qgis.utils.iface.zoomToActiveLayer()
>>
>> # now i need to zoom to a "precise" scale
>> canvas = qgis.utils.iface.mapCanvas()
>> canvas.zoomScale(2500) # this works for the canvas not in the composer
>>
>> # load the template
>> template_file = file(template_path)
>> template_content = template_file.read()
>> template_file.close()
>> document = QDomDocument()
>> document.setContent(template_content)
>> composition = QgsComposition(canvas.mapSettings())
>> composition.loadFromTemplate(document)
>> map_item = composition.getComposerItemById('map')
>> map_item.setMapCanvas(canvas)
>>
>> # at this point i need to set the extent to fit the layer
>> map_item.zoomToExtent(canvas.extent()) # this will not be in scale 2500
>> of course
>>
>> now i need the scale to be say 2500 but i cannot find how to
>> set the value
>>
>> thanks for any help, Roy
>>
>> _______________________________________________
>> Qgis-developer mailing list
>> [hidden email]
>> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> _______________________________________________
> Qgis-developer mailing list
> [hidden email]
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer

_______________________________________________
Qgis-developer mailing list
[hidden email]
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer