From: starscream <ni...@ho...> - 2013-01-14 09:31:22
|
Hi all, I'm working on a school project and untill now I have written my code, thanks to geotools tutorials, to build a map, and it works perfectly! Now I want to know how can I add some polygons on the layer of the map without losing them when I zoom in/out? I want to do this because I have an Excel file with some cities coordinates and I want to show them on the map! I've written this code: import java.awt.Color; import java.awt.Point; import java.io.File; import org.geotools.data.FeatureSource; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.geometry.jts.WKTReader2; import org.geotools.map.DefaultMapContext; import org.geotools.map.FeatureLayer; import org.geotools.map.Layer; import org.geotools.map.MapContext; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.geotools.styling.PolygonSymbolizer; import org.geotools.styling.SLD; import org.geotools.styling.StyleBuilder; import org.geotools.swing.JMapFrame; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.style.Style; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.util.GeometricShapeFactory; public class GIS{ public static void main(String[] args) throws Exception { usingFeatureCaching(); } public static void usingFeatureCaching() throws Exception { File file = new File("C:\\PE\\reg2011_g.shp"); FileDataStore store = FileDataStoreFinder.getDataStore(file); FeatureSource featureSource = store.getFeatureSource(); // Creates a map and adds the shape file MapContext map = new DefaultMapContext(); map.setTitle("La mappa dei Comuni d'Italia"); //Creates the map style StyleBuilder sb = new StyleBuilder(); PolygonSymbolizer restrictedSymb = sb.createPolygonSymbolizer(Color.LIGHT_GRAY, Color.BLACK, 0); //Sets opacity restrictedSymbreg2011_g.shp.getFill().setOpacity(sb.literalExpression(0.5)); org.geotools.styling.Style restrictedStyle = sb.createStyle(restrictedSymb); //Adds the layer to the map map.addLayer(featureSource, restrictedStyle); map.addLayer(getCitiesLayer()); //????????????? // Visualizza la Mappa JMapFrame.showMap(map); } public static Layer getCitiesLayer(){ SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder(); GeometryFactory geomFactory = new GeometryFactory(); b.setName( "pictures" ); b.setCRS( DefaultGeographicCRS.WGS84); //picture location b.add( "geom", Point.class ); final SimpleFeatureType TYPE = b.buildFeatureType(); SimpleFeatureCollection collection = FeatureCollections.newCollection("internal"); WKTReader2 wkt = new WKTReader2(); // Simple point coordinates (is an example) float lat = (float) 508469.7040; float lng = (float) 4358409.7367; com.vividsolutions.jts.geom.Point point1 = geomFactory.createPoint( new Coordinate(lat,lng)); //on this lin there is an error which dont let me compile the program collection.add(SimpleFeatureBuilder.build(TYPE, new Object[]{ point1, "first"}, null)); Style style = SLD.createPointStyle("Star", Color.BLUE, Color.BLUE, 0.3f, 15); Layer citiesLayer = new FeatureLayer(collection, (org.geotools.styling.Style) style); citiesLayer.setTitle("Cities layer"); return citiesLayer; } } Can you help me please? -- View this message in context: http://osgeo-org.1560.n6.nabble.com/How-to-add-polygons-on-existing-map-tp5027259.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. |