Revision: 6585
http://sourceforge.net/p/jump-pilot/code/6585
Author: ma15569
Date: 2020-10-08 10:27:49 +0000 (Thu, 08 Oct 2020)
Log Message:
-----------
rewrite class in order to speedup the process. Added some output method
(saveToFile and getRaster)
Modified Paths:
--------------
core/trunk/src/org/openjump/core/rasterimage/algorithms/RasterizeAlgorithm.java
core/trunk/src/org/openjump/core/ui/plugin/tools/generate/RasterizePlugIn.java
Modified:
core/trunk/src/org/openjump/core/rasterimage/algorithms/RasterizeAlgorithm.java
===================================================================
---
core/trunk/src/org/openjump/core/rasterimage/algorithms/RasterizeAlgorithm.java
2020-10-06 21:04:17 UTC (rev 6584)
+++
core/trunk/src/org/openjump/core/rasterimage/algorithms/RasterizeAlgorithm.java
2020-10-08 10:27:49 UTC (rev 6585)
@@ -45,106 +45,109 @@
*/
+
public class RasterizeAlgorithm {
- private static Double noData= -99999.0D;
+ private static Double noData = -99999.0D;
private static Double cellSize;
private static double dValue;
private static int m_iNX;
private static int m_iNY;
private static GridExtent m_Extent;
- public static WritableRaster raster;
-
-
- //[Giuseppe Aruta 2020-10-4] used to methods to rasterize:
+ private static WritableRaster raster;
+ private static FeatureCollection featureCollection;
+ private static String attrName;
+ private static double[][] values;
+ private static Envelope env;
+
+ public RasterizeAlgorithm(Envelope limitEnvelope, FeatureCollection
fCollection,
+ String attributeName, double CellSize) throws
OutOfMemoryError, Exception {
+ featureCollection = fCollection;
+ env=limitEnvelope;
+ attrName=attributeName;
+ cellSize=CellSize;
+ m_Extent= new GridExtent();
+ m_Extent.setCellSize(CellSize, CellSize);
+ double minX = limitEnvelope.getMinX();
+ double minY = limitEnvelope.getMinY();
+ double maxX = limitEnvelope.getMaxX();
+ double maxY = limitEnvelope.getMaxY();
+ m_Extent.setXRange(minX, maxX);
+ m_Extent.setYRange(minY, maxY);
+ m_iNX = m_Extent.getNX();
+ m_iNY = m_Extent.getNY();
+ values= new double[m_iNX][m_iNY];
+ for (int x = 0; x < m_iNX; x++){
+ for (int y = 0; y < m_iNY; y++){
+ values[x][y]=noData;
+ }
+ }
+ }
+
+
+ //[Giuseppe Aruta 2020-10-4] used to methods to rasterize:
// a) From AdbToolbox. Quite efficient with different types of geometries
but slow
// b) From Sextante, almost faster but still not working with ponts and
linestrings
- // The code below simplefies the access to the the method
+ // The code below simplefies the access to the the method
/**
- * Convert a feature collection to a raster according to a numeric
attribute, a limi envelope
- * and a cell size. Raster is saved as TIFF file.
- * Feature are first selected by the envelope and merged according the
- * attribute, then converted to a java.awt.image.WritableRaster
- * @param File file to save
- * @param Envelope limitEnvelope
- * @param FeatureCollection fCollection
- * @param String attributeName
- * @param double CellSize
- * @throws OutOfMemoryError
- * @throws Exception
+ * Convert a feature collection to a raster according to a numeric
attribute, a limit envelope
+ * and a cell size. Feature are first selected by the envelope and merged
according
+ * the chosen attribute, then converted to a grid
*/
- public static void Rasterize(File file, Envelope limitEnvelope,
FeatureCollection fCollection,
- String attributeName, double CellSize) throws
OutOfMemoryError, Exception {
- FeatureCollection fc = fCollection;
- FeatureCollection fc2 = getFeaturesOverlappingEnvelope(fc,
limitEnvelope, attributeName);
- FeatureCollection fc3 = unionByAttributeValue(fc2,
attributeName);
- if (!LayerableUtil.isPolygonalLayer(fCollection)) {
- RasterizeAlgorithm.RasterizeAdbToolbox(file, limitEnvelope, fc3,
attributeName, CellSize);
- } else {
- RasterizeAlgorithm.RasterizeSextante(file, limitEnvelope,
fc3, attributeName, CellSize);
- }
+ public void process() throws OutOfMemoryError, Exception {
+
+ FeatureCollection fc2 = getFeaturesOverlappingEnvelope();
+ FeatureCollection fc3 = unionByAttributeValue(fc2);
+ if (!LayerableUtil.isPolygonalLayer(fc3)) {
+ RasterizeAlgorithm.RasterizeAdbToolbox(fc3);
+ } else {
+ RasterizeAlgorithm.RasterizeSextante(fc3);
+ }
+
}
-
-
-
-
- /**
- * Method to rasterize a FeatureCollection using AdBToolboxframework
+
+ /**
+ * Method to save results to a TIFF file
* @param file
- * @param limitEnvelope
- * @param fCollection
- * @param attributeName
- * @param CellSize
- * @throws OutOfMemoryError
+ * @throws IOException
+ */
+
+ public void saveToFile(File file) throws IOException {
+ Envelope m_Envelope = new Envelope(m_Extent.getXMin(),
m_Extent.getXMax(),m_Extent.getYMin(), m_Extent.getYMax() );
+ RasterImageIO rasterImageIO = new RasterImageIO();
+ rasterImageIO.writeImage(file, raster, m_Envelope,
+ rasterImageIO.new CellSizeXY(cellSize, cellSize), noData);
+ }
+
+ /**
+ * Rasterize a FeatureCollection using AdBToolboxframework
* @throws Exception
*/
- private static void RasterizeAdbToolbox(File file, Envelope
limitEnvelope, FeatureCollection fCollection,
- String attributeName, double CellSize) throws
OutOfMemoryError, Exception {
- cellSize=CellSize;
- m_Extent= new GridExtent();
- m_Extent.setCellSize(CellSize, CellSize);
- double minX = limitEnvelope.getMinX();
- double minY = limitEnvelope.getMinY();
- double maxX = limitEnvelope.getMaxX();
- double maxY = limitEnvelope.getMaxY();
- m_Extent.setXRange(minX, maxX);
- m_Extent.setYRange(minY, maxY);
- minX = m_Extent.getXMin();
- minY = m_Extent.getYMin();
- maxX = m_Extent.getXMax();
- maxY = m_Extent.getYMax();
- m_iNX = m_Extent.getNX();
- m_iNY = m_Extent.getNY();
- double[][] valori= new double[m_iNX][m_iNY];
- for (int x = 0; x < m_iNX; x++){
- for (int y = 0; y < m_iNY; y++){
- valori[x][y]=noData;
- }
- }
- final Iterator<?> iter = fCollection.iterator();
+ private static void RasterizeAdbToolbox(FeatureCollection fCollection)
throws OutOfMemoryError, Exception {
+ final Iterator<?> iter = fCollection.iterator();
while (iter.hasNext()) {
Feature feature = (Feature) iter.next();
- dValue =
Double.parseDouble(feature.getAttribute(attributeName).toString());
- double[][] rasterized = new double[m_iNX][m_iNY];
- rasterized = rasterize(feature.getGeometry(), cellSize);
+ try {
+ dValue =
Double.parseDouble(feature.getAttribute(attrName).toString());
+ } catch (Exception e) {
+ dValue = noData;
+ }
+ double[][] rasterized = new double[m_iNX][m_iNY];
+ rasterized = rasterize(feature.getGeometry());
for (int r = 1; r < m_iNX; r++) {
for (int c = 1; c < m_iNY; c++) {
if (rasterized[r][c] == 1) {
- valori[r][c] = dValue;
+ values[r][c] = dValue;
}
}
}
}
- raster = GridRasterWrapper.matrixToRaster(valori);
- Envelope m_Envelope = new Envelope(minX, maxX,minY, maxY );
- RasterImageIO rasterImageIO = new RasterImageIO();
- rasterImageIO.writeImage(file, raster, m_Envelope,
- rasterImageIO.new CellSizeXY(CellSize, CellSize), noData);
+ raster = GridRasterWrapper.matrixToRaster(values);
}
-
-
+
+
/**
* Converts World coordinates to grid coordinates
* @param Coordinate coordinate
@@ -160,161 +163,25 @@
}
+
- private static double[][] rasterize(Geometry geom, double cellSize)
- throws Exception, OutOfMemoryError {
- double[][] grid = new double[m_iNX+2][m_iNY+2];
- for(int row = 0; row <= m_iNY; row++){
- for(int col = 0; col <= m_iNX; col++){
- grid[col][row] = 0;
- }
- }
- BufferedImage bimage = new BufferedImage(m_iNX, m_iNY,
BufferedImage.TYPE_INT_ARGB);
- bimage.setAccelerationPriority(1.0f);
- Graphics2D graphics = bimage.createGraphics();
- Coordinate[] coord;
- int[] coordGridX;
- int[] coordGridY;
- Color color = new Color(100);
- graphics.setPaint(color);
- graphics.setPaintMode();
- for(int g=0; g<geom.getNumGeometries(); g++){
- // Check if polygons has holes
-
if(geom.getGeometryN(g).getGeometryType().equals("Polygon")){
- Polygon polygon = (Polygon) geom.getGeometryN(g);
- java.awt.geom.Area awtArea;
- if(polygon.getNumInteriorRing() > 0){
- // Holes found
- // Exterior ring
- coord =
polygon.getExteriorRing().getCoordinates();
- coordGridX = new int[coord.length];
- coordGridY = new int[coord.length];
- // From geographic coords to image coords
- for(int p=0; p<coord.length; p++){
- java.awt.Point point =
fromCoordToCell(coord[p], cellSize);
- coordGridX[p] = point.x;
- coordGridY[p] = point.y;
- }
- java.awt.Polygon awtPolygon = new
java.awt.Polygon(coordGridX, coordGridY, coord.length);
- awtArea = new java.awt.geom.Area(awtPolygon);
- // Subtract inner rings
- for(int ir=0;
ir<polygon.getNumInteriorRing(); ir++){
- coord =
polygon.getInteriorRingN(ir).getCoordinates();
- coordGridX = new int[coord.length];
- coordGridY = new int[coord.length];
- // From geographic coords to image coords
- for(int p=0; p<coord.length; p++){
- java.awt.Point point =
fromCoordToCell(coord[p], cellSize);
- coordGridX[p] = point.x;
- coordGridY[p] = point.y;
- }
- awtPolygon = new
java.awt.Polygon(coordGridX, coordGridY, coord.length);
- java.awt.geom.Area awtArea2 = new
java.awt.geom.Area(awtPolygon);
- awtArea.subtract(awtArea2);
- }
- }else{
- coord = polygon.getCoordinates();
- coordGridX = new int[coord.length];
- coordGridY = new int[coord.length];
- // From geographic coords to image coords
- for(int p=0; p<coord.length; p++){
- java.awt.Point point =
fromCoordToCell(coord[p], cellSize);
- coordGridX[p] = point.x;
- coordGridY[p] = point.y;
- }
- java.awt.Polygon awtPolygon = new
java.awt.Polygon(coordGridX, coordGridY, coord.length);
- awtArea = new java.awt.geom.Area(awtPolygon);
- }
-
- graphics.setPaint(color);
- graphics.setPaintMode();
- graphics.draw(awtArea);
- graphics.fill(awtArea);
- }else{
- coord = geom.getGeometryN(g).getCoordinates();
- coordGridX = new int[coord.length];
- coordGridY = new int[coord.length];
-
- // From geographic coords to image coords
- for(int p=0; p<coord.length; p++){
- java.awt.Point point =
fromCoordToCell(coord[p], cellSize);
- coordGridX[p] = point.x;
- coordGridY[p] = point.y;
- }
-
if(geom.getGeometryN(g).getGeometryType().equals("LineString") ||
geom.getGeometryN(g).getGeometryType().equals("MultiLineString")){
- graphics.setPaint(color);
- graphics.setPaintMode();
- graphics.drawPolyline(coordGridX, coordGridY,
coord.length);
- }else
if(geom.getGeometryN(g).getGeometryType().equals("Point") ||
geom.getGeometryN(g).getGeometryType().equals("MultiPoint")){
- graphics.setPaint(color);
- graphics.setPaintMode();
- graphics.fillRect(coordGridX[0],
coordGridY[0], 1, 1);
- }
- }
- }
- for(int r=0; r<m_iNY; r++){
- for(int c=0; c<m_iNX; c++){
- if(bimage.getRGB(c, r) != 0 && bimage.getRGB(c,
r) != -1){
-
- grid[c+1][m_iNY-r] = 1;
- }
- }
- }
- return grid;
- }
-
-
/**
- * Method to rasterize a FeatureCollection using Sextante framework
- * @param file
- * @param Envelope limitEnvelope
- * @param Layer m_Layer
- * @param String attributeName
- * @param double CellSize
- * @param double NoData
- * @throws IOException
+ * Method Rasterize a FeatureCollection using Sextante framework
+ * @throws Exception
+ * @throws OutOfMemoryError
*/
- private static void RasterizeSextante(File file, Envelope
limitEnvelope, FeatureCollection fCollection,
- String attributeName, double CellSize ) throws
IOException {
+ private static void RasterizeSextante(FeatureCollection fCollection
) throws OutOfMemoryError, Exception {
- cellSize=CellSize;
- m_Extent= new GridExtent();
- m_Extent.setCellSize(CellSize, CellSize);
- double minX = limitEnvelope.getMinX();
- double minY = limitEnvelope.getMinY();
- double maxX = limitEnvelope.getMaxX();
- double maxY = limitEnvelope.getMaxY();
- m_Extent.setXRange(minX, maxX);//limitEnvelope.getMaxX());
- m_Extent.setYRange(minY, maxY);//limitEnvelope.getMaxY());
- minX = m_Extent.getXMin();
- minY = m_Extent.getYMin();
- maxX = m_Extent.getXMax();
- maxY = m_Extent.getYMax();
- //Recalculate cols and rows to avoid resampling of cell size
- m_iNX = m_Extent.getNX();
- m_iNY = m_Extent.getNY();
- double[][] valori= new double[m_iNX][m_iNY];
-
- for (int x = 0; x < m_iNX; x++){
- for (int y = 0; y < m_iNY; y++){
- valori[x][y]=noData;
-
- }
- }
- raster = GridRasterWrapper.matrixToRaster(valori);
- final Coordinate[] coords = new Coordinate[5];
- coords[0] = new Coordinate(minX, minY);
- coords[1] = new Coordinate(minX, maxY);
- coords[2] = new Coordinate(maxX, maxY);
- coords[3] = new Coordinate(maxX, minY);
- coords[4] = new Coordinate(minX, minY);
-
-
+ raster = GridRasterWrapper.matrixToRaster(values);
+ final Coordinate[] coords = new Coordinate[5];
+ coords[0] = new Coordinate(m_Extent.getXMin(),
m_Extent.getYMin());
+ coords[1] = new Coordinate(m_Extent.getXMin(),
m_Extent.getYMax());
+ coords[2] = new Coordinate(m_Extent.getXMax(),
m_Extent.getYMax());
+ coords[3] = new Coordinate(m_Extent.getXMax(),
m_Extent.getYMin());
+ coords[4] = new Coordinate(m_Extent.getXMin(),
m_Extent.getYMin());
final GeometryFactory gf = new GeometryFactory();
- final LinearRing ring = gf.createLinearRing(coords);
- final Polygon extent = gf.createPolygon(ring, null);
-
-
+ final LinearRing ring = gf.createLinearRing(coords);
+ final Polygon extent = gf.createPolygon(ring, null);
List<Feature> inputC = fCollection.getFeatures();
FeatureSchema schema = fCollection.getFeatureSchema();
FeatureDataset inputFC = new FeatureDataset(inputC, schema);
@@ -321,30 +188,31 @@
for (Iterator<Feature> it = inputFC.iterator() ; it.hasNext() ; )
{
Feature f = it.next();
try {
- dValue =
Double.parseDouble(f.getAttribute(attributeName).toString());
+ dValue =
Double.parseDouble(f.getAttribute(attrName).toString());
} catch (Exception e) {
- dValue = noData;
+ dValue = noData;
}
-
- final Geometry geometry = f.getGeometry();
-
-
- if (geometry.intersects(extent)) {
-
- doGeometry(geometry);
- }
+ final Geometry geometry = f.getGeometry();
+ if (geometry.intersects(extent)) {
+ doGeometry(geometry);
+ }
}
- RasterImageIO rasterImageIO = new RasterImageIO();
- rasterImageIO.writeImage(file, raster,
extent.getEnvelopeInternal(),
- rasterImageIO.new CellSizeXY(CellSize, CellSize),noData
);
+ }
-
+ /**
+ * Gets java.awt.image.WritableRaster
+ * @return java.awt.image.WritableRaster
+ */
+ public WritableRaster getRaster() {
+ return raster;
}
-
-
- private static void doPolygon(final Geometry geom) {
-
- final GeometryFactory gf = new GeometryFactory();
+
+
+
+
+
+ private static void doPolygon(final Geometry geom) {
+ final GeometryFactory gf = new GeometryFactory();
for (int i = 0; i < geom.getNumGeometries(); i++) {
final Polygon poly = (Polygon) geom.getGeometryN(i);
LinearRing lr =
gf.createLinearRing(poly.getExteriorRing().getCoordinates());
@@ -356,14 +224,12 @@
doPolygonPart(part, true);
}
}
+ }
- }
-
private static void doPolygonPart(final Polygon geom,
final boolean bIsHole) {
-
- boolean bFill;
+ boolean bFill;
boolean bCrossing[];
int x, y, ix, xStart, xStop, iPoint;
double yPos;;
@@ -370,38 +236,29 @@
Coordinate pLeft, pRight, pa, pb;
final Coordinate p = new Coordinate();
bCrossing = new boolean[m_iNX];
-
- final Envelope extent = geom.getEnvelopeInternal();
-
+ final Envelope extent = geom.getEnvelopeInternal();
xStart = (int) ((extent.getMinX()- m_Extent.getXMin()) /
m_Extent.getCellSize().x) - 1;
if (xStart < 0) {
xStart = 0;
}
-
- xStop = (int) ((extent.getMaxX()- m_Extent.getXMin()) /
m_Extent.getCellSize().x) + 1;
+ xStop = (int) ((extent.getMaxX()- m_Extent.getXMin()) /
m_Extent.getCellSize().x) + 1;
if (xStop >= m_iNX) {
xStop = m_iNX - 1;
}
-
- final Coordinate[] points = geom.getCoordinates();
-
- for (y = 0, yPos = m_Extent.getYMax(); y < m_iNY; y++, yPos -=
m_Extent.getCellSize().y) {
+ final Coordinate[] points = geom.getCoordinates();
+ for (y = 0, yPos = m_Extent.getYMax(); y < m_iNY; y++, yPos -=
m_Extent.getCellSize().y
+ ) {
if ((yPos >= extent.getMinY()) && (yPos <= extent.getMaxY())) {
Arrays.fill(bCrossing, false);
pLeft = new Coordinate(m_Extent.getXMin() - 1.0, yPos);
pRight = new Coordinate(m_Extent.getXMax() + 1.0, yPos);
-
- pb = points[points.length - 1];
-
+ pb = points[points.length - 1];
for (iPoint = 0; iPoint < points.length; iPoint++) {
pa = pb;
pb = points[iPoint];
-
if ((((pa.y <= yPos) && (yPos < pb.y)) || ((pa.y > yPos)
&& (yPos >= pb.y)))) {
getCrossing(p, pa, pb, pLeft, pRight);
-
ix = (int) ((p.x - m_Extent.getXMin()) /
m_Extent.getCellSize().x + 1.0);
-
if (ix < 0) {
ix = 0;
}
@@ -408,11 +265,9 @@
else if (ix >= m_iNX) {
ix = m_iNX - 1;
}
-
bCrossing[ix] = !bCrossing[ix];
}
}
-
for (x = xStart, bFill = false; x <= xStop; x++) {
if (bCrossing[x]) {
bFill = !bFill;
@@ -422,13 +277,11 @@
if (bIsHole) {
if (dPrevValue == dValue) {
raster.setSample(x, y, 0, noData);
-
}
}
else {
if (dPrevValue == noData) {
raster.setSample(x, y, 0, dValue);
-
}
}
}
@@ -435,23 +288,17 @@
}
}
}
-
}
-
-
+
private static void doLine(final Geometry geom) {
-
for (int i = 0; i < geom.getNumGeometries(); i++) {
final Geometry part = geom.getGeometryN(i);
doLineString(part);
}
-
}
-
private static void doLineString(final Geometry geom) {
-
int i;
double x, y, x2, y2;
final Coordinate[] coords = geom.getCoordinates();
@@ -462,21 +309,16 @@
y2 = coords[i + 1].y;
writeSegment(x, y, x2, y2);
}
-
}
-
private static void writeSegment(double x,
double y,
final double x2,
final double y2) {
-
double dx, dy, d, n;
GridCell cell;
-
dx = Math.abs(x2 - x);
dy = Math.abs(y2 - y);
-
if ((dx > 0.0) || (dy > 0.0)) {
if (dx > dy) {
dx /= cellSize;
@@ -490,38 +332,31 @@
dx /= dy;
dy = cellSize;
}
-
if (x2 < x) {
dx = -dx;
}
-
if (y2 < y) {
dy = -dy;
}
-
for (d = 0.0; d <= n; d++, x += dx, y += dy) {
if (m_Extent.contains(x, y)) {
+ // final GridCell cell =
m_Extent.getGridCoordsFromWorldCoords(coord.x, coord.y);
+ // java.awt.Point cell1 =
fromCoordToCell(new Coordinate(x,y), cellSize, m_Extent);
cell =
m_Extent.getGridCoordsFromWorldCoords(x, y);
//System.out.println(cell.getX() + "
" + cell.getY());
-
- raster.setSample(cell.getX(),
cell.getY(), 0,dValue);
+ raster.setSample(cell.getX(), cell.getY(),
0,dValue);
}
}
}
-
}
private static void doPoint(final Geometry geometry) {
-
final Coordinate coord = geometry.getCoordinate();
- final GridCell cell =
m_Extent.getGridCoordsFromWorldCoords(coord.x, coord.y);
- raster.setSample(cell.getX(), cell.getY(), 0,dValue);
-
+ // final GridCell cell =
m_Extent.getGridCoordsFromWorldCoords(coord.x, coord.y);
+ java.awt.Point cell = fromCoordToCell(coord, cellSize);
+ raster.setSample(cell.x, cell.y, 0,dValue);
}
-
-
-
private static boolean getCrossing(final Coordinate crossing,
@@ -529,7 +364,6 @@
final Coordinate a2,
final Coordinate b1,
final Coordinate b2) {
-
double lambda, div, a_dx, a_dy, b_dx, b_dy;
a_dx = a2.x - a1.x;
a_dy = a2.y - a1.y;
@@ -545,7 +379,7 @@
}
- public static void doGeometry (Geometry geometry) {
+ private static void doGeometry (Geometry geometry) {
if (geometry.getGeometryType().equals("Point") ||
geometry.getGeometryType().equals("MultiPoint")) {
doPoint(geometry);
} else if (geometry.getGeometryType().equals("LineString") ||
geometry.getGeometryType().equals("MultiLineString")) {
@@ -565,84 +399,184 @@
}
}
}
+
- public static FeatureCollection
unionByAttributeValue(FeatureCollection featureCollection, String value) throws
Exception {
- FeatureDataset outputFC = new
FeatureDataset(featureCollection.getFeatureSchema());
-
- Map<Object, FeatureCollection> map = new
HashMap<Object, FeatureCollection>();
- Iterator<Feature> itFeat=
featureCollection.getFeatures().iterator();
- while (itFeat.hasNext()) {
- Feature feature = itFeat.next();
- Object key = feature.getAttribute(value);
- if (!map.containsKey(key)) {
- FeatureCollection fd = new
FeatureDataset(featureCollection.getFeatureSchema());
- fd.add(feature);
- map.put(key, fd);
- } else {
- map.get(key).add(feature);
- }
- }
-
- Iterator<Object> iter = map.keySet().iterator();
- while (iter.hasNext()) {
- Object key = iter.next();
- FeatureCollection fca = map.get(key);
- if (fca.size() > 0) {
- Feature feature = union(fca);
- feature.setAttribute(value, key);
- outputFC.add(feature);
- }
- }
-
-
- return outputFC;
+
+ private static FeatureCollection
unionByAttributeValue(FeatureCollection featureCollection) throws Exception {
+ FeatureDataset outputFC = new
FeatureDataset(featureCollection.getFeatureSchema());
+
+ Map<Object, FeatureCollection> map = new HashMap<Object,
FeatureCollection>();
+ Iterator<Feature> itFeat=
featureCollection.getFeatures().iterator();
+ while (itFeat.hasNext()) {
+ Feature feature = itFeat.next();
+ Object key = feature.getAttribute(attrName);
+ if (!map.containsKey(key)) {
+ FeatureCollection fd = new
FeatureDataset(featureCollection.getFeatureSchema());
+ fd.add(feature);
+ map.put(key, fd);
+ } else {
+ map.get(key).add(feature);
+ }
}
-
-
-
-
- private static Feature union(FeatureCollection fc) {
- GeometryFactory factory = new GeometryFactory();
- Collection<Geometry> geometries = new
ArrayList<Geometry>();
- for (Feature f : fc.getFeatures()) {
- Geometry g = f.getGeometry();
- geometries.add(g);
- }
- Geometry unioned = UnaryUnionOp.union(geometries);
- FeatureSchema schema = fc.getFeatureSchema();
- Feature feature = new BasicFeature(schema);
- if (geometries.size()==0) {
-
feature.setGeometry(factory.createGeometryCollection(new Geometry[]{}));
- }
- else {
- feature.setGeometry(unioned);
- }
- return feature;
- }
-
-
-
- public static FeatureCollection
getFeaturesOverlappingEnvelope(FeatureCollection featureCollection,
- Envelope env, String attributeName) throws
Exception {
- Collection<Feature> inputC =
featureCollection.getFeatures();
- FeatureSchema schema1 =
featureCollection.getFeatureSchema();
- FeatureDataset inputFC = new FeatureDataset(inputC,
schema1);
-
-
- FeatureSchema schema = new FeatureSchema();
- schema.addAttribute("GEOMETRY",
AttributeType.GEOMETRY);
- schema.addAttribute(attributeName,
AttributeType.DOUBLE);
- FeatureDataset outputFC = new FeatureDataset(schema);
- GeometryFactory factory = new GeometryFactory();
- Geometry geom = factory.toGeometry(env);
-
- for (Feature f : inputFC.getFeatures()) {
- Geometry g = f.getGeometry();
- if (!geom.disjoint(g)){
- outputFC.add(f);}
- }
- return outputFC;
- }
+ Iterator<Object> iter = map.keySet().iterator();
+ while (iter.hasNext()) {
+ Object key = iter.next();
+ FeatureCollection fca = map.get(key);
+ if (fca.size() > 0) {
+ Feature feature = union(fca);
+ feature.setAttribute(attrName, key);
+ outputFC.add(feature);
+ // tree_map.put(feature.getID(), key.toString());
+ }
+ }
+
+
+ return outputFC;
+ }
+
+
+ private static Feature union(FeatureCollection fc) {
+ GeometryFactory factory = new GeometryFactory();
+ Collection<Geometry> geometries = new ArrayList<Geometry>();
+ for (Feature f : fc.getFeatures()) {
+ Geometry g = f.getGeometry();
+ geometries.add(g);
+ }
+ Geometry unioned = UnaryUnionOp.union(geometries);
+ FeatureSchema schema = fc.getFeatureSchema();
+ Feature feature = new BasicFeature(schema);
+ if (geometries.size()==0) {
+ feature.setGeometry(factory.createGeometryCollection(new
Geometry[]{}));
+ }
+ else {
+ feature.setGeometry(unioned);
+ }
+ return feature;
+ }
+
+
+
+ private static FeatureCollection getFeaturesOverlappingEnvelope() throws
Exception {
+ Collection<Feature> inputC =
featureCollection.getFeatures();
+ FeatureSchema schema1 = featureCollection.getFeatureSchema();
+ FeatureDataset inputFC = new FeatureDataset(inputC, schema1);
+ FeatureSchema schema = new FeatureSchema();
+ schema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
+ schema.addAttribute(attrName, AttributeType.DOUBLE);
+ FeatureDataset outputFC = new FeatureDataset(schema);
+ GeometryFactory factory = new GeometryFactory();
+ Geometry geom = factory.toGeometry(env);
+ for (Feature f : inputFC.getFeatures()) {
+ Geometry g = f.getGeometry();
+ if (!geom.disjoint(g)){
+ outputFC.add(f);}
+ }
+ return outputFC;
+ }
+
+
+
+ private static double[][] rasterize(Geometry geom)
+ throws Exception, OutOfMemoryError {
+ double[][] grid = new double[m_iNX+2][m_iNY+2];
+ for(int row = 0; row <= m_iNY; row++){
+ for(int col = 0; col <= m_iNX; col++){
+ grid[col][row] = 0;
+ }
+ }
+ BufferedImage bimage = new BufferedImage(m_iNX, m_iNY,
BufferedImage.TYPE_INT_ARGB);
+ bimage.setAccelerationPriority(1.0f);
+ Graphics2D graphics = bimage.createGraphics();
+ Coordinate[] coord;
+ int[] coordGridX;
+ int[] coordGridY;
+ Color color = new Color(100);
+ graphics.setPaint(color);
+ graphics.setPaintMode();
+ for(int g=0; g<geom.getNumGeometries(); g++){
+ // Check if polygons has holes
+ if(geom.getGeometryN(g).getGeometryType().equals("Polygon")){
+ Polygon polygon = (Polygon) geom.getGeometryN(g);
+ java.awt.geom.Area awtArea;
+ if(polygon.getNumInteriorRing() > 0){
+ // Holes found
+ // Exterior ring
+ coord = polygon.getExteriorRing().getCoordinates();
+ coordGridX = new int[coord.length];
+ coordGridY = new int[coord.length];
+ // From geographic coords to image coords
+ for(int p=0; p<coord.length; p++){
+ java.awt.Point point = fromCoordToCell(coord[p],
cellSize);
+ coordGridX[p] = point.x;
+ coordGridY[p] = point.y;
+ }
+ java.awt.Polygon awtPolygon = new
java.awt.Polygon(coordGridX, coordGridY, coord.length);
+ awtArea = new java.awt.geom.Area(awtPolygon);
+ // Subtract inner rings
+ for(int ir=0; ir<polygon.getNumInteriorRing(); ir++){
+ coord = polygon.getInteriorRingN(ir).getCoordinates();
+ coordGridX = new int[coord.length];
+ coordGridY = new int[coord.length];
+ // From geographic coords to image coords
+ for(int p=0; p<coord.length; p++){
+ java.awt.Point point = fromCoordToCell(coord[p],
cellSize);
+ coordGridX[p] = point.x;
+ coordGridY[p] = point.y;
+ }
+ awtPolygon = new java.awt.Polygon(coordGridX,
coordGridY, coord.length);
+ java.awt.geom.Area awtArea2 = new
java.awt.geom.Area(awtPolygon);
+ awtArea.subtract(awtArea2);
+ }
+ }else{
+ coord = polygon.getCoordinates();
+ coordGridX = new int[coord.length];
+ coordGridY = new int[coord.length];
+ // From geographic coords to image coords
+ for(int p=0; p<coord.length; p++){
+ java.awt.Point point = fromCoordToCell(coord[p],
cellSize);
+ coordGridX[p] = point.x;
+ coordGridY[p] = point.y;
+ }
+ java.awt.Polygon awtPolygon = new
java.awt.Polygon(coordGridX, coordGridY, coord.length);
+ awtArea = new java.awt.geom.Area(awtPolygon);
+ }
+
+ graphics.setPaint(color);
+ graphics.setPaintMode();
+ graphics.draw(awtArea);
+ graphics.fill(awtArea);
+ }else{
+ coord = geom.getGeometryN(g).getCoordinates();
+ coordGridX = new int[coord.length];
+ coordGridY = new int[coord.length];
+
+ // From geographic coords to image coords
+ for(int p=0; p<coord.length; p++){
+ java.awt.Point point = fromCoordToCell(coord[p], cellSize);
+ coordGridX[p] = point.x;
+ coordGridY[p] = point.y;
+ }
+ if(geom.getGeometryN(g).getGeometryType().equals("LineString")
|| geom.getGeometryN(g).getGeometryType().equals("MultiLineString")){
+ graphics.setPaint(color);
+ graphics.setPaintMode();
+ graphics.drawPolyline(coordGridX, coordGridY, coord.length);
+ }else if(geom.getGeometryN(g).getGeometryType().equals("Point")
|| geom.getGeometryN(g).getGeometryType().equals("MultiPoint")){
+ graphics.setPaint(color);
+ graphics.setPaintMode();
+ graphics.fillRect(coordGridX[0], coordGridY[0], 1, 1);
+ }
+ }
+ }
+ for(int r=0; r<m_iNY; r++){
+ for(int c=0; c<m_iNX; c++){
+ if(bimage.getRGB(c, r) != 0 && bimage.getRGB(c, r) != -1){
+
+ grid[c+1][m_iNY-r] = 1;
+ }
+ }
+ }
+ return grid;
+ }
}
Modified:
core/trunk/src/org/openjump/core/ui/plugin/tools/generate/RasterizePlugIn.java
===================================================================
---
core/trunk/src/org/openjump/core/ui/plugin/tools/generate/RasterizePlugIn.java
2020-10-06 21:04:17 UTC (rev 6584)
+++
core/trunk/src/org/openjump/core/ui/plugin/tools/generate/RasterizePlugIn.java
2020-10-08 10:27:49 UTC (rev 6585)
@@ -292,8 +292,9 @@
FeatureCollection fc =
sourceLayer.getFeatureCollectionWrapper();
final File outFile = FileUtil.addExtensionIfNone(new
File(path), "tif");
monitor.report(RASTERIZING_VECTOR+"...");
- RasterizeAlgorithm.Rasterize(outFile, fix, fc, selAttribute,
cellValue);
-
+ RasterizeAlgorithm rasterize = new RasterizeAlgorithm(fix, fc,
selAttribute, cellValue);
+ rasterize.process();
+ rasterize.saveToFile(outFile);
String catName = StandardCategoryNames.WORKING;
try {
_______________________________________________
Jump-pilot-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel