Geospatial.java [src/java/lamps/utils] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lamps.utils;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import csip.SessionLogger;
import java.io.IOException;
import java.math.RoundingMode;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import org.geotools.data.DataStore;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.resources.coverage.IntersectUtils;
import org.opengis.feature.simple.SimpleFeature;
/**
*
* @author od
*/
public class Geospatial {
/**
*
* @param shapefile
* @return
* @throws IOException
*/
public static FeatureCollection featureCollectionFromShapefile(URL shapefile) throws IOException {
DataStore dataStore = new ShapefileDataStore(shapefile);
FeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
dataStore.dispose();
return featureSource.getFeatures();
}
/**
*
* @param AOI
* @param simpFeatureList
* @param what
* @param majority_size
* @param LOG
* @return
* @throws com.vividsolutions.jts.io.ParseException
*/
public static ArrayList<String> overlayResultList(Geometry AOI, ArrayList<SimpleFeature> simpFeatureList, String what, double majority_size, SessionLogger LOG) throws ParseException {
ArrayList<String> resultList = new ArrayList<>();
//LOG.info(" What :" + what);
if (simpFeatureList.size() < 100) {
for (SimpleFeature feature : simpFeatureList) {
Geometry geometry = (Geometry) feature.getDefaultGeometry();
geometry = Geospatial.roundCoordinates(geometry.convexHull());
AOI = Geospatial.roundCoordinates(AOI);
//AOI = AOI.getCentroid();
if (AOI.within(geometry) || AOI.equals((Geometry) geometry)) {
resultList.add(feature.getProperty(what).getValue().toString().trim());
break;
} else if (AOI.intersects(geometry) || AOI.crosses(geometry) || AOI.overlaps(geometry) || AOI.touches(geometry)) {
Geometry intersection = null;
intersection = AOI.intersection(geometry);
if (intersection instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon) intersection;
double sum_area_part = 0;
for (int uu = 0; uu < mp.getNumGeometries(); uu++) {
com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(uu);
Geometry gIntersection = IntersectUtils.intersection(g, AOI);
sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.getArea()) * 100);
}
if (!resultList.contains(feature.getProperty(what).getValue().toString()) && sum_area_part > majority_size) {
resultList.add(feature.getProperty(what).getValue().toString());
}
}
if (intersection instanceof Polygon) {
com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection;
Geometry gIntersection = IntersectUtils.intersection(g, AOI);
double sum_area_part = 0;
sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.getArea()) * 100);
if (!resultList.contains(feature.getProperty(what).getValue().toString()) && sum_area_part > majority_size) {
resultList.add(feature.getProperty(what).getValue().toString());
}
}
}
}
} else {
resultList.add("XX-Non-Name");
}
return resultList;
}
/**
*
* @param geometry
* @return
* @throws com.vividsolutions.jts.io.ParseException
*/
public static Geometry roundCoordinates(Geometry geometry) throws ParseException {
WKTReader reader = new WKTReader();
String comma_changed = "";
if (geometry.getCoordinates().length == 2) {
} else {
for (int i = 0; i < geometry.getCoordinates().length; i++) {
//LOG.info(" Coords :" + targetGeometry.getCoordinates()[i]);
String[] each_coords = geometry.getCoordinates()[i].toString().trim().replaceAll("\\)", "").replaceAll("\\(", "").split(",");
double x = Double.parseDouble(each_coords[0]);
double y = Double.parseDouble(each_coords[1]);
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
comma_changed = comma_changed + df.format(x) + " " + df.format(y) + ",";
if (i + 1 == geometry.getCoordinates().length) {
comma_changed = comma_changed + df.format(x) + " " + df.format(y);
}
}
geometry = reader.read("POLYGON((" + comma_changed + "))");
}
return geometry;
}
}