Does anyone know of a library in Java that can parse ESRI Shapefiles?

GeoTools will do it. There are a ton of jars and you don’t need most of them. However, reading the shapefile is just a few lines.

File file = new File("mayshapefile.shp");

try {
  Map<String, String> connect = new HashMap();
  connect.put("url", file.toURI().toString());

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

  System.out.println("Reading content " + typeName);

  FeatureSource featureSource = dataStore.getFeatureSource(typeName);
  FeatureCollection collection = featureSource.getFeatures();
  FeatureIterator iterator = collection.features();


  try {
    while (iterator.hasNext()) {
      Feature feature = iterator.next();
      GeometryAttribute sourceGeometry = feature.getDefaultGeometryProperty();
    }
  } finally {
    iterator.close();
  }

} catch (Throwable e) {}

Leave a Comment