3

I am new to geotools and i find bit difficult to plot a point using the latitude and longitude values. can any one help me out to solve my problem. I tried the following code but not helpful.By quickstart i can able to pass a shapefile and getting the map but unable to plot the lat long values on it.

   SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

         b.setName( "MyFeatureType" );
          b.setCRS( DefaultGeographicCRS.WGS84 );
          b.add( "location", Point.class ); 
          //building  the type
          final SimpleFeatureType TYPE = b.buildFeatureType();

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); 
        Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
          featureBuilder.add(point);
          SimpleFeature feature = featureBuilder.buildFeature( "fid.1" );
          DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal",TYPE);
          featureCollection.add(feature);

Adding to map:

List a=new ArrayList();
a.add(-155.2792);
a.add(67.3623); 
double longitude=(Double) a.get(0);
double latitude=(Double)a.get(0); 
File file = JFileDataStoreChooser.showOpenFile("shp", null); 
if (file == null) { return; } 
FileDataStore store = FileDataStoreFinder.getDataStore(file); 
SimpleFeatureSource featureSource = store.getFeatureSource(); 
MapContext map = new DefaultMapContext(); 
map.setTitle("Show Map"); 




Layer layer = new FeatureLayer(featureSource, null);
        map.addLayer(layer);
    map.addLayer(featureSource, null); 
    JMapFrame.showMap(map);
  • The code you have posted is creating a feature and looks fine - how do you add it to the map? – Ian Turton Feb 1 '16 at 9:17
  • Dont know how to add,and the main problem is with plotting the given lat and long on map(which is from a shapefile) – Siv Feb 1 '16 at 9:42
  • please edit your question to explain more fully what you are trying to do, show the code where you add the new features to the map – Ian Turton Feb 1 '16 at 9:43
  • I am dynamically taking the shapefile and i want to plot the lat long in to it – Siv Feb 1 '16 at 9:50
  • please use the edit button to change the question - there is no sign of how you add the point to the map – Ian Turton Feb 1 '16 at 9:53
1

There are three distinct steps here, 1st make the point (which you seem to be able to do), 2nd add it to a layer with a style, 3rd add that to the map. This does the first 2 steps:

static Layer addPoint(double latitude, double longitude) {
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

    b.setName("MyFeatureType");
    b.setCRS(DefaultGeographicCRS.WGS84);
    b.add("location", Point.class);
    // building the type
    final SimpleFeatureType TYPE = b.buildFeatureType();

    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    Point point = geometryFactory.createPoint(new Coordinate( latitude, longitude));
    featureBuilder.add(point);
    SimpleFeature feature = featureBuilder.buildFeature(null);
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
    featureCollection.add(feature);
    Style style = SLD.createSimpleStyle(TYPE,Color.red);

    Layer layer = new FeatureLayer(featureCollection, style);
    return layer;
  }

then add this to the QuickStart:

  Layer pLayer = addPoint(-155.27,67.3623);
  map.addLayer(pLayer);

which creates a small red point in Alaska.

enter image description here

  • When I try you answer, with version 19.0 of geotools, it gives me this error: Exception in thread "main" java.lang.NullPointerException at org.geotools.styling.SLD.createSimpleStyle(SLD.java:1940) – Dimitri Mar 21 '18 at 22:36
  • please ask a new question if you are having problems – Ian Turton Mar 22 '18 at 10:35
2

For anyone who attempts to use the snippet in the solution, make sure you use import com.vividsolutions.jts.geom.Point; not import java.awt.Point;

Looks like geotools uses Java reflection, and happily accepts either Point class at compile time. However, you will get a Runtime exception if you attempt to run it with the latter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.