2

This is my first question in stackoverflow so I hope that I am doing it in the right way.

I am using geotools to read a shapeFile (.shp) and I cannot find the function to get all the points of the polygon. By now I have the following code:

public class ImportShapeFileService implements ImportShapeFileServiceInterface {

    @Override
    public void loadShapeFile(File shapeFile) throws MdfException {

        FileDataStore store;
        try {
            store = FileDataStoreFinder.getDataStore(shapeFile);
            SimpleFeatureSource featureSource = store.getFeatureSource();
            SimpleFeatureCollection collection = featureSource.getFeatures();

            ReferencedEnvelope env = collection.getBounds();
            double left = env.getMinX();
            double right = env.getMaxX();
            double top = env.getMaxY();
            double bottom = env.getMinY();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I am getting the four bounds of the shapFile, but not the points of the polygon that contains, is it possible to do that?

Thank you.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Alex Blasco
  • 793
  • 11
  • 22
  • When I said the points I meant the latitude and longitude of each point of the figure. – Alex Blasco Oct 06 '17 at 15:28
  • `SimpleFeatureIterator` and examine the geometry, for [example](http://docs.geotools.org/stable/userguide/tutorial/geometry/geometrycrs.html)? – trashgod Oct 06 '17 at 17:59

1 Answers1

2

Depending on what you want to do with the points, I'd do something like:

try(SimpleFeatureIterator itr1 = features.features()){
  while(itr1.hasNext()){
     SimpleFeature f = itr1.next();
     Geometry g = (Geometry)f.getDefaultGeometry();
     Coordinate[] coords = g.getCoordinates();
     // do something with the coordinates
   }
}

If you must have Point rather than just the coordinates (and you are sure you have polygons then you could use:

  Geometry g = (Geometry)f.getDefaultGeometry();
  for(int i=0;i<g.getNumPoints();i++) {
    Point p=((Polygon)g).getExteriorRing().getPointN(i);
  }
Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • `g.getCoordinates()` doesn't exist anymore in version "23-SNAPSHOT". :/ – Neph Sep 05 '19 at 08:22
  • 1
    It seems to see https://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/Geometry.html#getCoordinates-- Geometry is a JTS object not a GeoTools one. – Ian Turton Sep 05 '19 at 08:31
  • I see, thanks! Do you happen to know how to get a `CoordinateXYMZ` object from it? `g.getCoordinate()` returns a normal `Coordinate` one that has an `M` value but it's `NaN`, even though the shapefile has properly set `M` values. – Neph Sep 05 '19 at 09:27
  • you need to ask a new question – Ian Turton Sep 05 '19 at 09:36
  • Yes, I planning on doing that if you didn't know the answer. – Neph Sep 05 '19 at 09:45