Wednesday, July 16, 2008

How to read a feature layer and script on it

I just added a v.read command to the console commands list. Currently it can read shapefiles and udig layers if launched inside a JGrass extended environment... in which case it also takes into account feature selection.

The console scripting code:

JGrassFeatureValueSet featureSet = null;

jgrass {
$featureSet = v.read --iflayer-in adige_bacini_bz20 --oshapefile-out *
}

FeatureCollection fColl = featureSet.getFeatureCollection();
FeatureIterator featureIterator = fColl.features();

print("Read " + fColl.size() + " features");

while( featureIterator.hasNext() ) {
Feature feature = featureIterator.next();
String id = feature.getID();
Geometry geometry = feature.getDefaultGeometry();
Coordinate[] coords = geometry.getCoordinates();

double area = geometry.getArea();
double length = geometry.getLength();

print("Feature with ID=" + id + " has area=" + area + " and length=" + length + " and consists of " + coords.length + " nodes.");
}



And two screenshots to show how it works:

1) select feature on layer:




2) launch the script:

No comments: