From: Adnila <Ad...@we...> - 2010-06-20 19:13:53
|
hey guys, I have a problem. I want to intersect two geometries. Afterwards I want to add to the attribute values from both "old" geometries to the new feature. How am I supposed to do that? For example: 1. feature: name=Elbe, watertemperature=16, depth=5 2. feature: adminUnit=Dresden, nationalLevel=4, NUTSCode=DED21 new feature: name=Elbe, watertemperature=16, depth=5, adminUnit=Dresden, nationalLevel=4, NUTSCode=DED21 in the code snippet I tried to solve it. The values are in the SimpleFeature copy. But I do not know how to get them. Has anybody an idea how to solve this? I tried already various loops. But the matter is the copy.setValue(Object) or copy.setValue(collection). I do not know what they are doing. I looks like they always only set the first entry of the copy values section. Can somebody help? package controller; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.List; import javax.swing.JOptionPane; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFactorySpi; import org.geotools.data.DefaultTransaction; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureWriter; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureIterator; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.opengis.feature.Property; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.type.AttributeDescriptor; import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.MultiLineString; import com.vividsolutions.jts.geom.MultiPoint; import com.vividsolutions.jts.geom.MultiPolygon; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.geom.Polygon; import view.GISViewer; public class Test_Sicherung { private FeatureCollection<SimpleFeatureType, SimpleFeature> featColl_input; private FeatureIterator<SimpleFeature> iterator_adminUnit; private FeatureIterator<SimpleFeature> iterator_input; private CoordReferSyst crs_INSPIRE; private SimpleFeature feature_adminUnit; private FeatureCollection<SimpleFeatureType, SimpleFeature> featColl_adminUnit; private Transaction transaction; private Shapefile shapefile; private File file; private FileDataStore store; private FeatureSource<SimpleFeatureType, SimpleFeature> featSource_input; private SimpleFeature feature; private DataStoreFactorySpi factory; private Map<String, Serializable> create; private DataStore newDataStore; private SimpleFeatureType featureType; private SimpleFeatureType schema; private FeatureWriter<SimpleFeatureType, SimpleFeature> writer; private SimpleFeature copy; private com.vividsolutions.jts.geom.Geometry geometry3; private File fileNew; private MultiPolygon multiPolygon; private MultiPolygon multiPolygon_adminUnit; private Object geometry; private MultiLineString multiLineString; private com.vividsolutions.jts.geom.Geometry geometry4; private MultiPoint multiPoint; private com.vividsolutions.jts.geom.Geometry geometry5; private Polygon polygon; private com.vividsolutions.jts.geom.Geometry geometry6; private LineString lineString; private com.vividsolutions.jts.geom.Geometry geometry7; private Point point; private com.vividsolutions.jts.geom.Geometry geometry8; private SimpleFeatureType schema_new; private Iterator<? extends Property> test; private Property test1; /** * default constructor */ public Test_Sicherung() { // auto-generated constructor stub } // methods /** * method to intersect two layer with each other and write it into a shape file * @return * @throws Exception */ public File intersect(GISViewer viewer, FeatureSource<SimpleFeatureType, SimpleFeature> featureSource) throws Exception { // initialize feature collection featColl_adminUnit = featureSource.getFeatures(); shapefile = new Shapefile(); crs_INSPIRE = new CoordReferSyst(); // create new shape file and reproject old one file = shapefile.reprojectToShapefile(viewer); store = FileDataStoreFinder.getDataStore(file); featSource_input = store.getFeatureSource(); featColl_input = featSource_input.getFeatures(); // create a new shape file with a slight modified schema fileNew = new File("TempFile.shp"); factory = new ShapefileDataStoreFactory(); create = new HashMap<String, Serializable>(); create.put("url", fileNew.toURI().toURL()); create.put("create spatial index", Boolean.TRUE); newDataStore = factory.createNewDataStore(create); schema = featSource_input.getSchema(); SimpleFeatureTypeBuilder sftb = new SimpleFeatureTypeBuilder(); sftb.init(schema); List<AttributeDescriptor> attributeList = new ArrayList<AttributeDescriptor>(); attributeList = featColl_adminUnit.getSchema().getAttributeDescriptors(); sftb.addAll(attributeList); schema_new = sftb.buildFeatureType(); featureType = SimpleFeatureTypeBuilder.retype(schema_new, crs_INSPIRE.crs_ETRS89_LAEA()); newDataStore.createSchema(featureType); // carefully open an iterator and writer to process the results transaction = new DefaultTransaction("Convert"); writer = newDataStore.getFeatureWriterAppend(featureType.getTypeName(), transaction); // initialize feature iterator iterator_adminUnit = featColl_adminUnit.features(); iterator_input = featColl_input.features(); try { while(iterator_adminUnit.hasNext()){ feature_adminUnit = iterator_adminUnit.next(); while(iterator_input.hasNext()) { feature = iterator_input.next(); copy = writer.next(); multiPolygon_adminUnit = (MultiPolygon) feature_adminUnit.getDefaultGeometry(); geometry = feature.getDefaultGeometry(); if (geometry instanceof MultiPolygon) { multiPolygon = (MultiPolygon) feature.getDefaultGeometry(); geometry3 = multiPolygon_adminUnit.intersection(multiPolygon); copy.setDefaultGeometry(geometry3); } if (geometry instanceof MultiLineString) { multiLineString = (MultiLineString) feature.getDefaultGeometry(); geometry4 = multiPolygon_adminUnit.intersection(multiLineString); copy.setDefaultGeometry(geometry4); test = feature_adminUnit.getValue().iterator(); while (test.hasNext()) { test1 = test.next(); copy.setValue(test1); } } if (geometry instanceof MultiPoint) { multiPoint = (MultiPoint) feature.getDefaultGeometry(); geometry5 = multiPolygon_adminUnit.intersection(multiPoint); copy.setDefaultGeometry(geometry5); } if (geometry instanceof Polygon) { polygon = (Polygon) feature.getDefaultGeometry(); geometry6 = multiPolygon_adminUnit.intersection(polygon); copy.setDefaultGeometry(geometry6); } if (geometry instanceof LineString) { lineString = (LineString) feature.getDefaultGeometry(); geometry7 = multiPolygon_adminUnit.intersection(lineString); copy.setDefaultGeometry(geometry7); } if (geometry instanceof Point) { point = (Point) feature.getDefaultGeometry(); geometry8 = multiPolygon_adminUnit.intersection(point); copy.setDefaultGeometry(geometry8); } writer.write(); } } transaction.commit(); JOptionPane.showMessageDialog(null, "Convert to Administrative Unit complete"); } catch (Exception problem) { problem.printStackTrace(); transaction.rollback(); JOptionPane.showMessageDialog(null, "Convert to Administrative Unit failed"); } finally { writer.close(); iterator_adminUnit.close(); iterator_input.close(); transaction.close(); copy(fileNew, file); fileNew.delete(); } return file; } /** * copies source file to destiny file, if destiny file does not exist, it is created */ public void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } } -- View this message in context: http://osgeo-org.1803224.n2.nabble.com/Intersect-two-features-and-modelling-new-one-including-attributes-tp5201837p5201837.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. |