Javi

GeoTools: extract lat/lon from shape file

Nov 4th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1.     public void bulkShpDumper(FeatureSource featureSource, String featureIdField,
  2.             Map<String /* featureIdField */, Map<String /* FieldID */, Object /* Value */>> data)
  3.             throws IOException {
  4.         long t0 = System.currentTimeMillis();
  5.         log.info("Loading shapefile into a memory cache.");
  6.         FeatureCollection<SimpleFeatureType, SimpleFeature> selectedFeatures = featureSource.getFeatures();
  7.         FeatureIterator<SimpleFeature> iterator = selectedFeatures.features();
  8.         List<AttributeDescriptor> descriptors = null;
  9.         while (iterator.hasNext() == true) {
  10.             SimpleFeature feature = iterator.next();
  11.             if (descriptors == null) {
  12.                 descriptors = feature.getFeatureType().getAttributeDescriptors();
  13.             }
  14.             Map<String, Object> dataRow = new HashMap<String, Object>();
  15.             for (int idx = 0; idx < descriptors.size(); idx++) {
  16.                 AttributeDescriptor desc = descriptors.get(idx);
  17.                 Object value = feature.getAttribute(desc.getName());
  18.                 dataRow.put(desc.getName().getLocalPart(), value);
  19.                 if (((desc.getName().getLocalPart().toLowerCase().contains("geom")) == true) && (value instanceof Point)) {
  20.                     //POINT (2.159601897944868 41.36384684287495)
  21.                     Point valueAsPoint = (Point) feature.getAttribute(desc.getName());
  22.                     dataRow.put("lat", valueAsPoint.getY());
  23.                     dataRow.put("lon", valueAsPoint.getX());
  24.                 }
  25.             }
  26.             data.put(String.valueOf(dataRow.get(featureIdField)), dataRow);
  27.         }
  28.         long tf = System.currentTimeMillis();
  29.         log.info(MessageFormat.format(
  30.                 "All shape features loaded into the cache ({0}).", tf-t0));
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment