From: Oliver G. <ol...@dr...> - 2009-10-21 14:58:58
|
Michael, Thank you for the code snipped. Last night I figured this out by alsointegrating a snipped from the QuickStart example which matches withyour code snipped. I passed out before posting on the forum. I look forward to being an active member on this forum. Below is my working code and POM. package org.geotools.demo.example; import org.geotools.data.DataUtilities; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; import org.geotools.feature.FeatureIterator; import com.vividsolutions.jts.geom.Geometry; import org.geotools.data.FeatureSource; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapContext; import org.geotools.swing.JMapFrame; public class DisplayForcedShape { public static void main(String[] args) throws Exception { final SimpleFeatureType TYPE = DataUtilities.createType("Location","location:Point,name:String"); // see createFeatureType(); FeatureCollection collection = FeatureCollections.newCollection(); double lon[] = {123.31, 0}; double lat[] = {48.4, 52}; String name[] = { "Point1", "Point2", "Point3" }; GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null); for(int i=0;i<=1;i++){ Point point = factory.createPoint( new Coordinate(lon[i],lat[i])); SimpleFeature feature = SimpleFeatureBuilder.build( TYPE, new Object[]{point, name[i]}, null ); collection.add( feature ); } FeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { SimpleFeature feature = (SimpleFeature)iterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); Coordinate[] coords = geometry.getCoordinates(); for( int i = 0; i < coords.length; i++ ) { System.out.println(coords[i]); } } } finally { if( iterator != null ){ // YOU MUST CLOSE THE ITERATOR! iterator.close(); } } FeatureSource featureSource = DataUtilities.source(collection); MapContext map = new DefaultMapContext(); map.setTitle("Quickstart"); map.addLayer(featureSource, null); // Now display the map JMapFrame.showMap(map); } } <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.geotools.demo.example</groupId> <artifactId>gttest1</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>gttest1</name> <url>http://maven.apache.org</url> <properties> <geotools.version>2.6-M3</geotools.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>${geotools.version}</version> </dependency> </dependencies> <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net repository</name> <url>http://download.java.net/maven/2</url> </repository> <repository> <id>osgeo</id> <name>Open Source Geospatial Foundation Repository</name> <url>http://download.osgeo.org/webdav/geotools/</url> </repository> <repository> <id>osgeo-snapshots</id> <name>Snapshot Repository</name> <snapshots> <enabled>true</enabled> </snapshots> <url>http://repo.opengeo.org/</url> </repository> </repositories> </project> Michael Bedward wrote: Hi Oliver, In GeoTools 2.6.0, if you just want to display your in-memory feature collection you can do something like this... FeatureCollection<SimpleFeatureType, SimpleFeature> collection = ... MapContext map = new DefaultMapContext(); map.setTitle("My feature collection"); map.addLayer(collection, null); JMapFrame.showMap(map); These are the imports: import org.geotools.feature.FeatureCollection; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapContext; import org.geotools.swing.JMapFrame; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; If you want to display your points with something other than the very basic default style you might want to look at the StyleLab example: http://geotools.org/examples/stylelab.html Hope that helps. Michael |