/** * @param poly * @param * @return value */ private static double evaluateSinglePolygon( Geometry poly, Quadtree fqTree, String attributeName, int attributeOp, int spatialRelation, double bufferRadius) { List items = SpatialRelationOp.evaluateSpatial(spatialRelation, fqTree, poly, bufferRadius); double val = AttributeOp.evaluateAttributes(attributeOp, items, attributeName); return val; }
/** * @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; }