1

Is it possible through geotools to identify and mark all the dangles and hanging roads(lines) in a .shp file representing a map. Any example would help me proceed forward.

So far I have found the below which gives me the number of dangles in the .shp files:

SimpleFeatureCollection col = featureSource.getFeatures();
SimpleFeatureIterator itr = col.features();
List list = new ArrayList();

while(itr.hasNext()){

SimpleFeature feature = itr.next();
Geometry geom = (Geometry) feature.getDefaultGeometry();
list.add(geom);
}

Polygonizer polygonizer = new Polygonizer();

polygonizer.add(list);

Collection dangles = polygonizer.getDangles();
Iterator it = dangles.iterator();
while (it.hasNext()) {
    System.out.println(it.next());;
}

Please let me know if we can locate these on a map. Possibly, I require to highlight these marking dangled lines.

1

JTS' Pologonizer returns a collection of the input LineStrings which are dangles. However, GeoTools prefers to display whole Features rather than raw geometries. So you either need to generate some new Features from the returned LineStrings or search through the input features to find matching Geometries.

So something like:

SimpleFeatureCollection col = featureSource.getFeatures();
List<Geometry> list = new ArrayList<>();
try (SimpleFeatureIterator itr = col.features()) {

  while (itr.hasNext()) {

    SimpleFeature feature = itr.next();
    Geometry geom = (Geometry) feature.getDefaultGeometry();
    list.add(geom);
  }
}
Polygonizer polygonizer = new Polygonizer();

polygonizer.add(list);

Collection dangles = polygonizer.getDangles();
TreeSet<LineString> set = new TreeSet<>(dangles);
List<SimpleFeature> features = new ArrayList<>();
try (SimpleFeatureIterator itr = col.features()) {
  while (itr.hasNext()) {

    SimpleFeature feature = itr.next();
    LineString geom = (LineString) feature.getDefaultGeometry();
    if (set.contains(geom)) {
      features.add(feature);
      set.remove(geom);
    }
  }
}
SimpleFeatureCollection newCollection = DataUtilities.collection(features);
| improve this answer | |

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.