/** * Copy/clone the input featureSchema since it is not proper implemented in Jump * * @param oldSchema * @return a clone of oldSchema */ public static FeatureSchema copyFeatureSchema(FeatureSchema oldSchema) { FeatureSchema fs = new FeatureSchema(); for (int i = 0; i < oldSchema.getAttributeCount(); i++) { AttributeType at = oldSchema.getAttributeType(i); String aname = oldSchema.getAttributeName(i); fs.addAttribute(aname, at); fs.setCoordinateSystem(oldSchema.getCoordinateSystem()); } return fs; }
/** * Copy the input feature to a new Schema whereby the new Feature Schema must be an extended or * shortened one * * @param feature * @param newSchema * @return a new Feature with newSchema as Schema and feature values */ public static Feature copyFeature(Feature feature, FeatureSchema newSchema) { FeatureSchema oldSchema = feature.getSchema(); Feature newF = new BasicFeature(newSchema); int n = 0; if (oldSchema.getAttributeCount() > newSchema.getAttributeCount()) { // for schema shortening n = newSchema.getAttributeCount(); } else { // for schema extension n = oldSchema.getAttributeCount(); } for (int i = 0; i < n; i++) { String aname = oldSchema.getAttributeName(i); Object value = feature.getAttribute(aname); newF.setAttribute(aname, value); } return newF; }
/** * @param sourceFeatures * @param targetFeatures * @param attributeName * @param attributeOp * @param spatialRelation * @param bufferRadius * @return a feature dataset */ public static FeatureDataset joinAttributes( Collection sourceFeatures, Collection targetFeatures, String attributeName, int attributeOp, int spatialRelation, double bufferRadius, TaskMonitor monitor) { /* System.out.println("Join Attributes --- attribute op:" + attributeOp + " - " + AttributeOp.getName(attributeOp) + " --- spatial op: " + spatialRelation + " - " + SpatialRelationOp.getName(spatialRelation)); */ FeatureDataset fd = null; AttributeType newAttributeType = AttributeType.DOUBLE; String newAttributeName = attributeName + "_" + AttributeOp.getName(attributeOp); if (attributeOp == AttributeOp.COUNT) { newAttributeName = AttributeOp.getName(attributeOp); } // -- put all in a tree Quadtree fqTree = new Quadtree(); FeatureSchema sourceFS = null; int count = 0; for (Iterator iter = sourceFeatures.iterator(); iter.hasNext(); ) { Feature pt = (Feature) iter.next(); fqTree.insert(pt.getGeometry().getEnvelopeInternal(), pt); if (count == 0) { sourceFS = pt.getSchema(); } count++; } // -- get AttributeType AttributeType at = null; try { at = sourceFS.getAttributeType(attributeName); } catch (Exception e) { at = AttributeType.GEOMETRY; attributeName = sourceFS.getAttributeName(0); System.out.println( "JoinAttributes.joinAttributes: replace unknown attribute name by geometry"); } // ArrayList outPolys = new ArrayList(); int size = targetFeatures.size(); FeatureSchema targetFSnew = null; count = 0; Iterator iterp = targetFeatures.iterator(); while (iterp.hasNext()) { count = count + 1; if (monitor != null) { monitor.report("item: " + count + " of " + size); } Feature p = (Feature) iterp.next(); if (count == 1) { FeatureSchema targetFs = p.getSchema(); targetFSnew = copyFeatureSchema(targetFs); if (targetFSnew.hasAttribute(newAttributeName)) { // attribute will be overwriten } else { // add attribute targetFSnew.addAttribute(newAttributeName, newAttributeType); } } // -- evaluate value for every polygon double value = evaluateSinglePolygon( p.getGeometry(), fqTree, attributeName, attributeOp, spatialRelation, bufferRadius); Feature fcopy = copyFeature(p, targetFSnew); fcopy.setAttribute(newAttributeName, new Double(value)); outPolys.add(fcopy); } fd = new FeatureDataset(targetFSnew); fd.addAll(outPolys); return fd; }