Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
eu.europa.ec.eurostat.jgiscotools.algo.base.Resolutionise Maven / Gradle / Ivy
/**
*
*/
package eu.europa.ec.eurostat.jgiscotools.algo.base;
import java.util.ArrayList;
import java.util.Collection;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.precision.GeometryPrecisionReducer;
import eu.europa.ec.eurostat.jgiscotools.feature.JTSGeomUtil;
/**
* @author julien Gaffuri
*
*/
public class Resolutionise {
public static Geometry get(Geometry g, double resolution) {
PrecisionModel pm = new PrecisionModel(1/resolution);
Geometry g2 = GeometryPrecisionReducer.reduce(g, pm);
while(!g2.isValid())
g2 = GeometryPrecisionReducer.reduce(g2, pm);
return g2;
//See https://github.com/locationtech/jts/issues/324
//MinimumClearance.getDistance
//snap-rounding
}
public static Collection getLine(LineString line, double resolution) {
Geometry line2 = get(line, resolution);
return JTSGeomUtil.getLineStrings(line2);
}
public static Collection getLine(Collection lines, double resolution) {
Collection out = new ArrayList<>();
for(LineString line : lines)
out.addAll( getLine(line, resolution) );
return out;
}
public static Collection getPoly(Polygon poly, double resolution) {
Geometry poly2 = get(poly, resolution);
return JTSGeomUtil.getPolygons(poly2);
}
public static Collection getPoly(Collection polys, double resolution) {
Collection out = new ArrayList<>();
for(Polygon poly : polys)
out.addAll( getPoly(poly, resolution) );
return out;
}
/*public Puntal puntal = null;
public Lineal lineal = null;
public Polygonal polygonal = null;*/
/*
public static void main(String[] args) throws Exception {
System.out.println("start");
WKTReader rdr = new WKTReader();
for(String ds : new String[] {"africa","europe","world","uk"}) {
System.out.println("*** "+ds);
WKTFileReader wfr = new WKTFileReader("src/test/resources/testdata/"+ds+".wkt", rdr);
Collection> gs = wfr.read();
Collection fs = new ArrayList<>();
for(Object g_ : gs) {
Geometry g = (Geometry)g_;
System.out.println(g.getGeometryType() + " " + g.getCoordinates().length);
for(double res : new double[] {0.001, 0.01, 0.1, 0.5, 1}) {
System.out.println(res);
Geometry g2 = get2(g, res);
System.out.println(g2.getGeometryType() + " " + g2.getCoordinates().length);
Feature f = new Feature();
g2 = JTSGeomUtil.toMulti(g2);
f.setGeometry(g2);
f.setAttribute("res", res);
fs.add(f);
}
}
GeoPackageUtil.save(fs, "target/"+ds+".gpkg", CRS.decode("EPSG:3035"), true);
}
System.out.println("end");
}
*/
/*
public static Geometry getSimple(Geometry g, double resolution) {
GeometryFactory gf = g.getFactory();
if(g instanceof Point) {
Geometry out = g.copy();
apply(out.getCoordinates(), resolution);
return out;
}
else if(g instanceof MultiPoint){
Geometry out = g.copy();
apply(out.getCoordinates(), resolution);
return out.union();
} else if(g instanceof LineString) {
Geometry out = g.copy();
apply(out.getCoordinates(), resolution);
out = out.union();
LineMerger merger = new LineMerger();
merger.add(out);
return gf.buildGeometry( merger.getMergedLineStrings() );
} else if(g instanceof MultiLineString) {
Geometry out = g.copy();
apply(out.getCoordinates(), resolution);
out = out.union();
LineMerger merger = new LineMerger();
merger.add(out);
out = gf.buildGeometry( merger.getMergedLineStrings() );
apply(out.getCoordinate(), resolution);
out = out.union();
merger = new LineMerger();
merger.add(out);
out = gf.buildGeometry( merger.getMergedLineStrings() );
return out;
} else if(g instanceof Polygon) {
Geometry shellRes = getSimple(((Polygon)g).getExteriorRing(), resolution);
Polygon p = gf.createPolygon(shellRes.getCoordinates());
//TODO remove holes one by one
//p = p.buffer(0);
return p;
//return out.buffer(0);
} else if(g instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon)g;
HashSet polys = new HashSet();
for(int i=0; i resRemoveDuplicateCoordsLinear(LineString line) {
if(line.getLength() == 0) return new HashSet<>();
Collection line_ = new HashSet<>(); line_.add(line);
Geometry u = Union.getLineUnion(line_);
return JTSGeomUtil.getLineStrings(u);
}
public static Collection applyLinear(LineString line, double resolution) {
//apply(line, resolution);
//return resRemoveDuplicateCoordsLinear(line);
}
*/
//base functions
/*public static Coordinate get(Coordinate c, double resolution){
return new Coordinate(
Math.round(c.x/resolution)*resolution,
Math.round(c.y/resolution)*resolution
);
}
public static Coordinate[] get(Coordinate[] cs, double resolution){
Coordinate[] cs_ = new Coordinate[cs.length];
for(int i=0; i gs, double resolution) {
for(Geometry g : gs) apply(g, resolution);
}
public static void apply(Geometry g, double resolution) {
apply(g.getCoordinates(), resolution);
}
public static void apply(Coordinate c, double resolution) {
c.x = ((int)Math.round(c.x/resolution)) * resolution;
c.y = ((int)Math.round(c.y/resolution)) * resolution;
}
public static void apply(Coordinate[] cs, double resolution) {
for(Coordinate c : cs) apply(c, resolution);
}
*/
/*
private static boolean samePosition(Coordinate c1, Coordinate c2) { return c1.x==c2.x && c1.y==c2.y; }
public static Coordinate[] removeDuplicates(Coordinate[] cs){
ArrayList csSorted = new ArrayList(Arrays.asList(cs));
Collections.sort(csSorted, new Comparator() {
public int compare(Coordinate c1, Coordinate c2) { return c1.x>c2.x?1:c1.y>c2.y?1:0; }
});
HashSet cs_ = new HashSet();
Coordinate cPrev = null;
for(Coordinate c : csSorted){
if(cPrev==null || !samePosition(c,cPrev)) cs_.add(c);
cPrev=c;
}
return cs_.toArray(new Coordinate[cs_.size()]);
}
public static Coordinate[] removeConsecutiveDuplicates(Coordinate[] cs){
ArrayList cs_ = new ArrayList();
Coordinate cPrev = null;
for(Coordinate c : cs){
if(cPrev==null || !samePosition(c,cPrev)) cs_.add(c);
cPrev=c;
}
return cs_.toArray(new Coordinate[cs_.size()]);
}*/
/* public static Collection resApplyLines(Collection lines, double res) {
Resolutionise.apply(lines, res);
return resRemoveDuplicateCoordsLinear(lines);
}
public static Collection resRemoveDuplicateCoordsLinear(Collection lines) {
Collection out = new HashSet<>();
for(Geometry line : lines) {
if(line.getLength() == 0) continue;
Collection line_ = new HashSet<>(); line_.add(line);
Geometry u = Union.getLineUnion(line_);
if(u.isEmpty()) continue;
if(u instanceof Point) continue;
out.addAll(JTSGeomUtil.getLineStringGeometries(u));
}
return out;
}
public static void main(String[] args) {
//tests
//TODO extract as true tests
GeometryFactory gf = new GeometryFactory();
//points
Point pt;
pt = gf.createPoint(new Coordinate(107.4, 502.78));
System.out.println(pt);
System.out.println(new Resolutionise(pt,1).puntal);
System.out.println(new Resolutionise(pt,10).puntal);
System.out.println(new Resolutionise(pt,100).puntal);
pt = gf.createPoint(new Coordinate(87.5, 502.78));
System.out.println(pt);
System.out.println(new Resolutionise(pt,1).puntal);
System.out.println(new Resolutionise(pt,10).puntal);
System.out.println(new Resolutionise(pt,100).puntal);*/
//multipoint
/*MultiPoint pt;
pt = gf.createMultiPoint(new Coordinate[] {new Coordinate(107.4, 502.78), new Coordinate(117.4, 500), new Coordinate(487.4, 1402.78)});
System.out.println(pt);
System.out.println(new Resolutionise(pt,1).puntal);
System.out.println(new Resolutionise(pt,10).puntal);
System.out.println(new Resolutionise(pt,100).puntal);
System.out.println(new Resolutionise(pt,1000).puntal);*/
/*/linestring
LineString ls;
ls = gf.createLineString(getCoordsArray(107.4, 502.78, 117.4, 500, 487.4, 1402.78));
System.out.println(ls);
System.out.println(new Resolutionise(ls,1).lineal);
System.out.println(new Resolutionise(ls,10).lineal);
System.out.println(new Resolutionise(ls,100).lineal);
System.out.println(new Resolutionise(ls,1000).puntal);
System.out.println("-------");
ls = gf.createLineString(getCoordsArray(107.4, 502.78, 117.4, 504, 120.4, 490, 107.4, 503));
System.out.println(ls);
System.out.println(new Resolutionise(ls,1).lineal);
System.out.println(new Resolutionise(ls,10).lineal);
System.out.println(new Resolutionise(ls,100).puntal);
System.out.println(new Resolutionise(ls,1000).lineal);
System.out.println("-------");
ls = gf.createLineString(getCoordsArray(0, 0, 1000,509, 1000, 500, 0, 1));
System.out.println(ls);
System.out.println(new Resolutionise(ls,10).lineal);
System.out.println(new Resolutionise(ls,100).lineal);
System.out.println("-------");
ls = gf.createLineString(getCoordsArray(0, 1, 1000,1, 1000, 0, 1, 0, 1, -100, 0, -100, 0, 0));
System.out.println(ls);
System.out.println(new Resolutionise(ls,1).lineal);
System.out.println(new Resolutionise(ls,10).lineal);
System.out.println(new Resolutionise(ls,100).lineal);
System.out.println("-------");
ls = gf.createLineString(getCoordsArray(-10,0, 0,0, 0,-10, 1,-10, 1,0, 10,0, 10,1, -10,1, -10, 0));
System.out.println(ls);
System.out.println(new Resolutionise(ls,10).lineal);
System.out.println(new Resolutionise(ls,100).puntal);
}*/
/*
public static Coordinate[] getCoordsArray(double ... data){
Coordinate[] cs = new Coordinate[data.length/2];
for(int i=0; i