1

I have a shapefile which has building footprints as polygon features. I want to read shapefile and extract these bbox of these polygon features using geotools. So far I read shapefile but I don't know how can I access geometry and attributes of a shapefile records using geotools. Here is my code; but I don't know how can I do the rest of work for my purpose.

package org.geotools;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Tiler {
File file = new File ("C:/Users/Ziya/Desktop/Kasustu/footprints.shp");
Map<String, URL> map = new HashMap<String, URL>();
public Tiler() throws IOException {
    map.put("url", file.toURI().toURL());
}

    DataStore dataStore= DataStoreFinder.getDataStore(map);
    SimpleFeatureSource featureSource = 
    dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
    SimpleFeatureCollection collection = featureSource.getFeatures();

1 Answer 1

2

Once you have a SimpleFeatureCollection it is just a case of iterating through the collection and looking at whichever attributes you need. The SimpleFeature interface is well documented.

try (SimpleFeatureIterator itr = (SimpleFeatureIterator) inputFeatureCollection.features()) {
  while (itr.hasNext()) {
    SimpleFeature f = itr.next();
    //Default geometery 
    Geometry g = (Geometry)f.getDefaultGeomerty();
    //bounding box
    Geometry envPoly = g.getEnvelope(); // a polygon
    Envelope env = geom.getEnvelopeInternal(); // a simple envelope
    double minx = env.getMinX();
    double miny = env.getMinY();
    double maxx = env.getMaxX();
    double maxy = env.getMaxY();

    //access properties or attributes
    Property p = f.getProperty("name");
    PropertyType type = p.getType();
    Name name = p.getName();
    Object v = p.getValue();
    List<Object> l = f.getAttributes();
    Object a1 = f.getAttribute("name");
    Object a2 = f.getAttribute(0);
  }
}

Also please use URLs.fileToUrl(inFile) instead of file.toURI().toURL() your windows users will thank you for it.

10
  • Sorry for dummy question but I come from python and both java and geotools are new world for me. How can I access coordinates of envelope. For example I want to create an array which consist of envelope coordinates for every feature??
    – user51044
    Feb 26, 2018 at 14:40
  • see the update - the real question is why do you want to this?
    – Ian Turton
    Feb 26, 2018 at 18:13
  • I want to exract coordinates of envelopes for every feaute because using them I will construct a rtree data structure. A rtree java implementation requires bounding boxes and their coordinates for rtree construction. Here is the link : github.com/davidmoten/rtree. These min and max coordinates are for only an envelope of they are as a list or array of every features envelop coordinates?
    – user51044
    Feb 26, 2018 at 18:24
  • did you look at SpatialIndexFeatureCollection?
    – Ian Turton
    Feb 26, 2018 at 18:39
  • Yes but i did'nt solve my problem. My final purpose is using RTree generating tiles for my data (input shapefile). I need all envelopes and their coordinates for non leaf child nodes and actual polygon features. So it seems SpatialIndexFeatureCollection doesn't return coordinates of non leaf child nodes. Am I wrong?
    – user51044
    Feb 26, 2018 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.