2

This is my first time using GeoTools and I have a 3 simple things I need to get from .shp files which I'm reading with my program.

I need to know in specific coordinates (latitude/longitude).

Example: lat=58.85 lon=5.26 for 3 specific things:

  1. Exclusive Economic Zone (EEZ)
  2. FAO area
  3. ICES statistical rectangle

I know that all of these properties are there, because tested the whole shapefile and it gave my these results:

EEZ<EEZ id=eez.232>=German Exclusive Economic Zone
EEZ<EEZ id=eez.233>=Azerbaijanis Exclusive Economic Zone
EEZ<EEZ id=eez.234>=Kazakh Exclusive Economic Zone

And same is for ICES rectangles:

ICESNAME<ICESNAME id=ices.11002>=26M8
ICESNAME<ICESNAME id=ices.11003>=27M8

It reads the whole shapefile, but I don't know how to read these 3 properties from specific coordinate.

My program code:

File file = new File(shapeDirectory + "ices.shp");

Map<String, Object> map = new HashMap<>();
map.put("url", file.toURI().toURL());

DataStore dataStore = DataStoreFinder.getDataStore(map);
String typeName = dataStore.getTypeNames()[0];

FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE; // ECQL.toFilter("BBOX(THE_GEOM, 10,20,30,40)")

SimpleFeatureType schema = source.getSchema();
String geomType = schema.getGeometryDescriptor().getType().getBinding().getName();
System.out.println(geomType);

    FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
    try (FeatureIterator<SimpleFeature> features = collection.features()) {
        while (features.hasNext()) {
            SimpleFeature feature = features.next();
            System.out.print(feature.getID());
            Property property = feature.getProperty("ICESNAME");
            System.out.println(property);
        }
    }
}

How to read all these 3 attributes (from 3 different shape files (fao/ices/eez)) at specific coordinates? The coordinate input will be decimal, but if someone knows, please provide me the degrees/minutes/seconds example too.

1 Answer 1

1

Basically you need a contains filter so that you get back the feature(s) which have a polygon that contains the point.

First, you need to know the name of the geometry attribute in your features:

Expression propertyName = filterFactory.property(features.getSchema()
            .getGeometryDescriptor().getName());

Then build a point p:

GeometryFactory fac = new GeometryFactory();
double lat = (Math.random() * 180.0) - 90.0;
double lon = (Math.random() * 360.0) - 180.0;
Point p = fac.createPoint(new Coordinate(lat, lon));

And then you need to build a filter with your point (p):

FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2(GeoTools
            .getDefaultHints());
Filter filter = filterFactory.contains(propertyName,
            filterFactory.literal(p));

And finally, fetch any features that match that filter:

SimpleFeatureCollection sub = features.subCollection(filter);

You then need to repeat those steps for each of your shapefiles for as many points as you need.

As for converting DMS to decimal degrees, this should do it:

public class DMSToDegrees {

  public static void main(String[] args) {
    System.out.println(DMSToDegrees.convert(37, 42, 17));
    System.out.println(DMSToDegrees.convert(89, 11, 15));
    System.out.println(DMSToDegrees.convert(-37, 42, 17));
  }

  static public double convert(int degrees, int minutes, double seconds) {
    float sign = Math.signum(degrees);
    double dmins = minutes+seconds/60.0;
    double deg = Math.abs(degrees) + dmins/60.0;
    return sign*deg;
  }
}
2
  • features.getSchem() does not work for me. Where to get/set FilterFactory. And how to create a point with specific coordinates?
    – Kefirchiks
    Commented Apr 6, 2018 at 10:01
  • My features is your source,
    – Ian Turton
    Commented Apr 6, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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