[Gvsig_english] Distance using Jython

Juan Lucas Dominguez Rubio jldominguez at prodevelop.es
Wed Dec 9 14:01:07 CET 2009


Hello.
To compute the distance between two features of the same layer, you can do this:
 
- Add the layer to the view
- Set the layer as the only active layer (by clicking on it in the TOC)
- Select the two features for which you wish to know the distance. You must have exactly two features selected
- Click on the menu option: "File - Scripting - Jython console"
- Paste this long line:
 
import com.iver.andami; import com.iver.andami.ui.mdiManager; v = com.iver.andami.PluginServices.getMDIManager().getActiveWindow(); mc = v.getMapControl(); mx = mc.getMapContext(); lyrs = mx.getLayers(); lyract = lyrs.getActives()[0]; selsup = lyract.getSelectionSupport(); sel = selsup.getSelection(); firstsel = sel.nextSetBit(0); secondsel = sel.nextSetBit(firstsel+1); vf = lyract.getSource(); fgeom = vf.getShape(firstsel); sgeom = vf.getShape(secondsel); fjtsgeom = fgeom.toJTSGeometry(); sjtsgeom = sgeom.toJTSGeometry(); fjtsgeom.distance(sjtsgeom)
 
- Then hit enter and you should see the distance in map units as seen here:
 
http://www.prodevelop.es/files/fm/public/downloads/images/jython_dist.jpg
 
For a more elegant way to do this, see:
 
ftp://downloads.gvsig.org/gva/descargas/manuales/gvsig-1_0-scripting-tutorial-v2-en.zip
 
I'm not saying this is the only way to do it, though.
 
The script shown above uses the distance function implemented in the widely used JTS library:
 
http://www.vividsolutions.com/Jts/javadoc/com/vividsolutions/jts/geom/Geometry.html#distance(com.vividsolutions.jts.geom.Geometry)
 
 
Regards,
 
Juan Lucas Domínguez Rubio
---
Prodevelop SL, Valencia (España)
Tlf.: 96.351.06.12 -- Fax: 96.351.09.68
http://www.prodevelop.es <http://www.prodevelop.es/> 
---
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserv.gva.es/pipermail/gvsig_internacional/attachments/20091209/91c6edf1/attachment.htm 


More information about the Gvsig_internacional mailing list