61

I'm interested in writing a visualization program for the road data in the 2009 Tiger/Line Shapefiles. I'd like to draw the line data to display all the roads for my county.

The ESRI Shapefile or simply a shapefile is a popular geospatial vector data format for geographic information systems software. It is developed and regulated by ESRI as a (mostly) open specification for data interoperability among ESRI and other software products.1 A "shapefile" commonly refers to a collection of files with ".shp", ".shx", ".dbf", and other extensions on a common prefix name (e.g., "lakes.*"). The actual shapefile relates specifically to files with the ".shp" extension, however this file alone is incomplete for distribution, as the other supporting files are required.

Does anyone know of existing libraries for parsing and reading in the line data for Shapefiles?

geographika
  • 6,458
  • 4
  • 38
  • 56
KingNestor
  • 65,976
  • 51
  • 121
  • 152

5 Answers5

77

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) {}
hb0
  • 3,350
  • 3
  • 30
  • 48
Clint
  • 8,988
  • 1
  • 26
  • 40
  • 4
    if you use Maven to compile the program it handles all the jars for you. – Ian Turton Feb 23 '11 at 18:31
  • 1
    Geotools has great tutorial on how to start development. It's a Swing application for visualizing a map http://docs.geotools.org/latest/userguide/tutorial/quickstart/netbeans.html – Rafal Rusin Jun 12 '11 at 12:58
  • This answers example is excellent, saved me having to deep dive into the API docs – Phil Ostler May 30 '12 at 16:09
9

Openmap has a Java API that provides read and write access to ESRI files.

Robert Christie
  • 20,177
  • 8
  • 42
  • 37
  • 1
    Note that although BBN's OpenMap is open source, it uses a weird BBN-specific license. It's pretty permissive, but the fact that it's not a standard license may cause issues when trying to combine it with other software (or when attempting to get it approved by your legal beagles). – Tom Morris Jul 27 '12 at 17:38
  • Can you provide an example of how to use said API to read an ESRI file? OpenMap's API is opaque and utterly baffling to me. – Dylan Knowles Dec 15 '14 at 22:45
6

There is GeoTools, or more exactly this class ShapefileDataStore.

adrian.tarau
  • 3,124
  • 2
  • 26
  • 29
3

You could try to use Java ESRI Shape File Reader library. It's small, easy to install and has very simple API. The only drawback is that it does not read other mandatory and optional files (.shx, .dbf, etc.) that are usually shipped with a shape file.

Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
0

You can directly use GUI GIS tools so that their is no need of changing the source code of GeoTools.

I use QGIS which does all operations(even more) than GeoTools.

Quantum GIS - An open source Geographic Information System for editing, merging and simplifying shapefile maps. See also: creating maps with multiple layers using Quantum GIS.