Plot Longitude and Latitudes on Map using GeoTools – How to Tutorial

geotoolsjavalatitude longitude

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);

Best Answer

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