Let's say I have a data class in Java that essentially has

  • title
  • geo (WKT String)
  • Other irrelevant metadata

I want to create shapefile from lists of these types of objects using something like GeoTools. What are my options for being able to do this?

Essentially I'm creating an exporter for an internal data model we have.


The GeoTools' feature tutorial shows how to do this for a CSV file to Shapefile. All you need to do is replace the createFeatureType method with some code that reflects your own object.

 * Here is how you can use a SimpleFeatureType builder to create the schema for your shapefile
 * dynamically.
 * <p>This method is an improvement on the code used in the main method above (where we used
 * DataUtilities.createFeatureType) because we can set a Coordinate Reference System for the
 * FeatureType and a a maximum field length for the 'name' field dddd
private static SimpleFeatureType createFeatureType() {

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system

    // add attributes in order
    builder.add("the_geom", Point.class);
    builder.length(15).add("Name", String.class); // <- 15 chars width for name field
    builder.add("number", Integer.class);

    // build the type
    final SimpleFeatureType LOCATION = builder.buildFeatureType();

    return LOCATION;

and the section that creates the new features:

     * A list to collect features as we create them.
    List<SimpleFeature> features = new ArrayList<>();

     * GeometryFactory will be used to create the geometry attribute of each feature,
     * using a Point object for the location.
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        /* First line of the data file is the header */
        String line = reader.readLine();
        System.out.println("Header: " + line);

        for (line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.trim().length() > 0) { // skip blank lines
                String tokens[] = line.split("\\,");

                double latitude = Double.parseDouble(tokens[0]);
                double longitude = Double.parseDouble(tokens[1]);
                String name = tokens[2].trim();
                int number = Integer.parseInt(tokens[3].trim());

                /* Longitude (= x coord) first ! */
                Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));

                SimpleFeature feature = featureBuilder.buildFeature(null);

With something that converts the WKT to a geometry object:

WKTReader wktR = new WKTReader();
Geometry geom = wktR.read(wkt);

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.