Class ShapefileFeatureLoader

java.lang.Object
edu.cnu.mdi.mapping.shapefile.ShapefileFeatureLoader

public final class ShapefileFeatureLoader extends Object
Loads arbitrary ESRI Shapefile data into ShapeFeature instances without any assumptions about the attribute schema or geometry type.

This loader is the generic counterpart to the schema-specific ShapefileCountryLoader and ShapefileCityLoader. It is appropriate for any shapefile layer whose content is not known at compile time — rivers, lakes, urban areas, administrative boundaries at any level, protected areas, coastlines, and so on.

Supported geometry types

Null-shape records are silently skipped.

Attribute loading

All .dbf fields are included in each feature's property map. Values are trimmed strings; callers use ShapeFeature.getProperty(String) to retrieve them by field name. No field names are required or assumed.

Coordinate handling

Input coordinates must be WGS84 or NAD83/GRS80 geographic degrees (longitude, latitude order). NAD83 is treated as WGS84 without datum transformation, which is appropriate for map display. If a companion .prj exists, it is validated and incompatible or unrecognized coordinate systems are rejected. If it is absent, WGS84 is assumed. Coordinates are converted to radians and longitude is wrapped to (-π, π] before being stored.

File layout contract

The .dbf companion file is derived from the .shp path by replacing the extension. Both files must reside in the same directory with the same base name. The .shx index file is not required; records are read sequentially from the .shp file directly.

Usage


 ShapefileFeatureLoader loader = new ShapefileFeatureLoader();

 // Load rivers
 List<ShapeFeature> rivers = loader.load(Path.of("ne_10m_rivers_lake_centerlines.shp"));
 ShapeFeatureStyle riverStyle = new ShapeFeatureStyle()
         .strokeColor(new Color(0x6B9FD4))
         .strokeWidth(0.8f);
 mapView.addLayer(new ShapeFeatureRenderer(rivers, riverStyle));

 // Load lakes
 List<ShapeFeature> lakes = loader.load(Path.of("ne_10m_lakes.shp"));
 ShapeFeatureStyle lakeStyle = new ShapeFeatureStyle()
         .fillColor(new Color(0x6B9FD4))
         .strokeColor(new Color(0x4A7FB5))
         .strokeWidth(0.5f)
         .labelField("name");
 mapView.addLayer(new ShapeFeatureRenderer(lakes, lakeStyle));
 

This class is stateless and thread-safe; a single instance may be reused across multiple load(java.nio.file.Path) calls.

  • Constructor Details

    • ShapefileFeatureLoader

      public ShapefileFeatureLoader()
      Creates a new loader. No configuration is required; all behaviour is determined by the shapefile itself and the ShapeFeatureStyle applied at render time.
  • Method Details

    • load

      public List<ShapeFeature> load(Path shpPath) throws IOException
      Loads all features from the shapefile at the given path.

      Opens the .shp geometry file and the companion .dbf attribute file (derived by replacing the extension). Both files are read sequentially and the results are combined into one ShapeFeature per shapefile record.

      Parameters:
      shpPath - path to the .shp file; the companion .dbf must exist in the same directory with the same base name; must not be null
      Returns:
      unmodifiable list of ShapeFeature instances in file order; never null
      Throws:
      IOException - if either file cannot be opened or a parse error occurs