[Qgis-developer] VectorFileWriter

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Nov 26 05:20:03 EST 2007


Nicolas B. wrote:

> The code works but i still have the following error message just before the
> created shp loads " Note: the following lines were not loaded because Qgis
> was unable to determine values for the x and y coordinates:"
> I don't understand why. Any ideas ??

  Does it say which 'following lines' were not loaded? Does your csv 
file have any bad data in it? Is there a header line in the CSV that 
perhaps Qgis doesn't skip? Have you tried the standard Qgis CSV reader 
plugin to see how that reacts?

> The main problem is if i use my plugin again, the shp doesn't want to be
> created again because it already exists in the data folder.
> I'd like to destroy the previous existing folder every time the
> VectorFileWriter is launched. How can i do that ?

  Use some python functions for testing file existence?

  >>> os.path.exists("/etc/passwd")
  True
  >>> os.path.exists("/etc/qoowoowowo")
  False

Then use python functions for deleting files - os.unlink() is what you 
want here. Check out the python docs on file handling, and don't forget 
to delete .shp, .shx and .dbf when deleting shapefiles!

> Finally, i'd like to apply symbology properties (graduated colors, field to
> use, number of classes...) on the loaded shp.
> Is there a Qgs class to do this ?

  Its all in the API: http://svn.qgis.org/api_doc/html/classes.html

  You have to define a renderer, set its properties - this code should 
define a continuous colour renderer that goes from green to red:

         r=QgsContinuousColorRenderer(layer.vectorType())
         smin=QgsSymbol(layer.vectorType(), "0","","")
         smax=QgsSymbol(layer.vectorType(), "100", "","")
         smin.setPen(QPen(Qt.green, 1.0))
         smax.setPen(QPen(Qt.red, 1.0))
         r.setMinimumSymbol(smin)
         r.setMaximumSymbol(smax)
         r.setClassificationField(fieldNumber)

   Then set the renderer on the layer:

  layer.setRenderer(r)

[note this code was chopped out of a larger system and so somethings 
might not work...]

  Barry



More information about the Qgis-developer mailing list