gvSIG 2.0: Scripting, exploit your gvSIG (II). Creating a buffer

In some previous posts we’ve seen how to create an easy script on gvSIG 2.0.

Now we show a video about how to implement a script to create a polygon shapefile that is obtained after applying a buffer to the geometries of a point layer. The buffer distance is obtained from a field of the point layer.

The steps to follow the script are:

  • We get the input layer.

  • We create the data definition (Schema) of the new layer.
  • We get the creation data of the new layer.
  • We create the new layer.
  • We scan the features of the input layer (points).
  • We create the new geometries.
  • We add the new feature to the new layer.
  • We finish the editing of the new layer.
  • We release the group of features of the input layer

The source code with comments is:

from gvsig import *

def main():
  #We get the active layer
  layer = currentLayer()

  #We create the data definition
  schema = createSchema(layer.getSchema())
  schema.modify()

  #We get the projection
  crs = currentProject().getProjectionCode()

  #We define the path where the new layer
  ruta = "/home/victor/carto/influencia.shp"

  #We create the layer
  newLayer = createShape(
    schema,
    ruta,
    CRS = crs,
    geometryType = SURFACE
  )

  #We get the features of the input layer
  features = layer.features()

  #We scan all the features 
  for feature in features:

    # We get the value of the buffer
    influencia = feature.get("Influencia")

    # We create the geometry coming of the buffer on the feature geometry
    geom = feature.geometry().buffer(influencia)

    # We get the values of the input feature attributes
    values = feature.getValues()

    # We add the new geometry in the new layer
    values["GEOMETRY"] = geom
    newLayer.append(values)

  # We finish the editing of the new layer saving changes
  newLayer.commit()

  # We release the group of features of the input layer
  features.dispose()

You have to take into account that it’s necessary to use gvSIG 2.0 Final version and the Scripting Framework extension to work without problems. Although the view must have an active layer and this layer has to have a double type field called Influencia. And better, you can modify the script to adapt it to your data.

We hope you enjoyed it.

 

This entry was posted in english, gvSIG Desktop, scripting. Bookmark the permalink.

Leave a comment