Never mind... stupid question. Just needed to use the getAttribute() method.
On Sun, Oct 3, 2010 at 7:29 AM, Jason Ferguson <[email protected]>wrote:
> I am trying to write code to import the TIGER shapefiles, which of course
> have multiple fields in different orders.
>
> The solution seems to be the index field in SimpleFeatureImpl, but it has
> protected access. A quick look at the source doesn't show any method to
> retrieve it.
>
> Is there an easy way to get access to that field without checking out the
> whole project and modifying the one source file?
>
> Here is my source (snipped from a Struts2 action). Right now, the code will
> only successfully process a County or Equivalent shapefile. If I could
> access the index, I could handle multiple types.
>
> BorderPoint is a simple POJO tagged as a Hibernate entity.
>
> ShapefileDataStore store = new ShapefileDataStore(shpFile.toURL());
> String[] typenames = store.getTypeNames();
>
> for (String name : typenames) {
> FeatureSource featureSource = store.getFeatureSource(name);
> FeatureCollection featureCollection =
> featureSource.getFeatures();
> FeatureIterator iterator = featureCollection.features();
>
> try {
>
> // FeatureCollection doesn't implement Iterator, so no
> forEach() loop for me. Jerks.
> StateCode stateCode = null;
> Integer stateCodeId = null;
> while (iterator.hasNext()) {
> SimpleFeatureImpl feature = (SimpleFeatureImpl)
> iterator.next();
>
> SimpleFeatureImpl.class.getDeclaredField("index").setAccessible(true);
>
> int stateId = feature.index.get("MTFCC"); // DOESNT
> WORK SINCE index IS PROTECTED
> // TODO: CREATE THE BORDERPOINT HERE!!!
> // process a county
> String lineType = (String) feature.getAttribute(9);
> if (lineType.equals("G4020")) {
> // get the StateCode.. statecode isn't going to
> change since user is required to upload
> // state-based shapefiles
> if (stateCodeId == null) {
> stateCodeId = Integer.parseInt((String)
> feature.getAttribute(1));
> stateCode =
> stateCodeService.getByStateCode(stateCodeId);
> }
>
> int subCodeId = Integer.parseInt((String)
> feature.getAttribute(2));
> String subCodeDescription = (String)
> feature.getAttribute(5);
>
> SubCode subCode = new SubCode(stateCode, subCodeId,
> subCodeDescription, "County");
> SubCode result = subCodeService.save(subCode);
>
> //BorderPointFactory bpFactory = new
> BorderPointFactory(stateCode, subCode);
>
> MultiPolygon mp = (MultiPolygon)
> feature.getAttribute(0);
> Geometry g = mp.getGeometryN(0);
> Coordinate[] coordinates = g.getCoordinates();
>
> Set<BorderPoint> bpSet = new
> HashSet<BorderPoint>();
> for (Coordinate c : coordinates) {
>
> Float lat = new Float(c.x);
> Float lng = new Float(c.y);
>
> lat = MathUtils.round(lat, 3);
> lng = MathUtils.round(lng, 3);
>
> if (lat != null && lng != null) {
> BorderPoint bp = new BorderPoint(stateCode,
> subCode, lat, lng);
> bpSet.add(bp);
>
> }
> }
>
> borderPointService.saveAll(bpSet);
>
> }
> }
> }
> finally {
> featureCollection.close(iterator);
>
> }
> }
>
------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users