Hello,

Firstly, please try to be more specific with your questions: for
example, "I want to do X but when I run my app it does Y", or "my code
fails to compile with the following error trace..." etc.  People here
want to help you, but if you just post code with a general "please
help" message you are less likely to get useful answers.

I'm not sure what you mean when you say: add some polygons that won't
disappear when you zoom. Your getCitiesLayer method seems to be trying
to create a layer of points - yes ?  As long as you only call that
once when you are setting up the layers to display things those points
should not disappear.

There is a bug in your getCitiesLayer method: you are setting Point
and String values when creating a feature but you have forgotten to
add the String attribute to your feature type.

Please use MapContent in your code instead of the deprecated
MapContext / DefaultMapContext classes. It is easier and more
reliable. You should also avoid using the deprecated
FeatureCollection.add method. For the approach that is now recommended
please see the Features tutorial:

http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html

I hope that helps a bit.

Michael


On 14 January 2013 20:31, starscream <niko...@hotmail.it> wrote:
> 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.
>
> ------------------------------------------------------------------------------
> Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
> MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
> with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
> MVPs and experts. SALE $99.99 this month only -- learn more at:
> http://p.sf.net/sfu/learnmore_122412
> _______________________________________________
> GeoTools-GT2-Users mailing list
> GeoTools-GT2-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to