Noli,

Right, zsj's script is what you want - its fun and works quite well.

But to make it work you need to launch and application process (like QGIS does) 
and attach the widget.

I've placed the code here for you to get at:



http://bitbucket.org/dodobas/haosbuilder/
On Jul 22, 2010, at 7:42 PM, Noli Sicad wrote:

> Hi,
> 
> I have been looking for simple application in python i..e. PyQt that
> has map canvas to render  shapefile or spatialite.

Well, given styling logic, right?

Thats the nice thing about QGIS - it gives you a pretty solid environment to 
create the styles.

> 
> I like QGIS, however, qgis.core and qgis.gui is only available when
> you installed QGIS application. It is not a regular python module.
> 

Well, it should be possible, but ya, its rarely done and depending on the build 
of QGIS you are using could be tricky in terms of library paths (mac osx in 
particular)


> I like to see a simple PyQt4 application with mapnik canvas viewer
> without PyQGIS (QGIS API).
> 
> I have been reading the archive of mapnik. I saw these postings.
> http://www.mail-archive.com/[email protected]/msg02736.html
> 
> zsj,/ feverzsj,  is your "simple pyqt4 widget" that are working
> running now?

Yes, zsj's app is very simple, but quite nice. Other examples using wxpython 
include:

http://bitbucket.org/dodobas/haosbuilder/
http://trac.mapnik.org/wiki/IntegrateWithWxPython


> Do you have a complete code to this program now? I run
> your scripts appended the postings, no errors but what do you suppose
> to see? No widget or qt form being rendered.

Right, because the script needs to launch an application. I will attach the 
modified code which does that.

Use it like:

$ pyqtmapnik.py /path/to/map.xml


> 
> http://www.mail-archive.com/[email protected]/msg02719.html
> http://bitbucket.org/springmeyer/quantumnik/src/tip/quantumnik.py#cl-592
> 
> Dane, would be possilbe to have just PyQt with mapnik without  QGIS example?
> 
zsj's code does that.

> Hope see simple PyQt application with Mapnik viewer / canvas running.
> 

Let us know how the attached code goes. It requires a working Mapnik XML 
stylesheet.

If you need one, grab the one try this one:

http://bitbucket.org/springmeyer/tilelite/src/tip/demo/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import mapnik

from PyQt4.QtCore import *
from PyQt4.QtGui import *



class MapnikWidget(QWidget):

   def __init__(self, parent = None):

       QWidget.__init__(self, parent)

       self.map = mapnik.Map(256, 256)
       self.qim = QImage()
       self.startDragPos = QPoint()
       self.endDragPos   = QPoint()
       self.drag         = False
       self.scale        = False
       self.timer        = QTimer()

       self.timer.timeout.connect(self.updateMap)

       self.total_scale = 1.0

   def load_map(self, xml):
       self.map = mapnik.Map(256, 256)
       mapnik.load_map(self.map, xml)
       self.map.resize(self.width(), self.height())
       self.zoom_all()

   def close_map(self):
       self.map = mapnik.Map(256, 256)
       self.updateMap()

   def updateMap(self):
       self.timer.stop()
       self.total_scale = 1.0
       self.scale       = False

       im = mapnik.Image(self.map.width, self.map.height)
       mapnik.render(self.map, im)
       self.qim = QImage(im.tostring(),self.map.width, self.map.height,QImage.Format_ARGB32).rgbSwapped()
       #self.qim.loadFromData(QByteArray(im.tostring('png')))
       self.update()

   def paintEvent(self, event):
       painter = QPainter(self)

       if self.drag:
           painter.drawImage(self.endDragPos - self.startDragPos, self.qim)
       elif self.scale:
           qw = self.qim.width()
           qh = self.qim.height()
           newWidth = int(qw * self.total_scale)
           newHeight = int(qh * self.total_scale)
           newX = (qw - newWidth) / 2
           newY = (qh - newHeight) / 2
           painter.save()
           painter.translate(newX, newY)
           painter.scale(self.total_scale, self.total_scale)
           exposed = painter.matrix().inverted()[0].mapRect(self.rect()).adjusted(-1, -1, 1, 1)
           painter.drawImage(exposed, self.qim, exposed)
           painter.restore()
       else:
           painter.drawImage(0, 0, self.qim)

       painter.setPen(QColor(0, 0, 0, 100))
       painter.setBrush(QColor(0, 0, 0, 100))
       painter.drawRect(0, 0, 256, 26)
       painter.setPen(QColor(0, 255 , 0))
       painter.drawText(10, 19, 'Scale Denominator: ' + str(self.map.scale_denominator()))

   def zoom_all(self):
       self.map.zoom_all()
       self.updateMap()

   def resizeEvent(self, event):
       self.map.resize(event.size().width(), event.size().height())
       self.updateMap()

   def wheelEvent(self, event):
       self.scale = True
       scale_factor = 1.0 - event.delta() / (360.0 * 8.0) * 4
       self.map.zoom(scale_factor)
       self.total_scale *= 1 / scale_factor
       self.update()
       self.timer.start(400)


   def mousePressEvent(self, event):
       if event.button() == Qt.LeftButton:
           self.startDragPos = event.pos()
           self.drag         = True


   def mouseMoveEvent(self, event):
       if self.drag:
           self.endDragPos = event.pos()
           self.update()

   def mouseReleaseEvent(self, event):
       if event.button() == Qt.LeftButton:
           self.drag = False
           self.endDragPos = event.pos()

           cx = int(0.5 * self.map.width)
           cy = int(0.5 * self.map.height)
           dpos = self.endDragPos - self.startDragPos
           self.map.pan(cx - dpos.x() ,cy - dpos.y())
           self.updateMap()


if __name__ == '__main__':
    if not len(sys.argv) > 1:
        sys.exit('please provide the path to a mapnik xml stylesheet')
    app = QApplication(sys.argv)
    w = MapnikWidget()
    w.show()
    w.load_map(sys.argv[1])
    sys.exit(app.exec_())
> Thanks.
> 
> Regards, Noli

You are welcome.

Dane


_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users

Reply via email to