use of oms3.annotations.In in project hortonmachine by TheHortonMachine.
the class Mapurl2MbtilesConverter method process.
@Execute
public void process() throws Exception {
checkNull(inFile);
File mapurlFile = new File(inFile);
HashMap<String, String> metadataMap = FileUtilities.readFileToHashMap(inFile, "=", false);
String url = metadataMap.get("url");
if (url == null) {
throw new ModelsIllegalargumentException("The supplied file doesn't seem to be a valid HortonMachine mapurl file.", this, pm);
}
if (url.endsWith("jpg")) {
format = "jpg";
} else {
format = "png";
}
File dbFile = FileUtilities.substituteExtention(mapurlFile, "mbtiles");
String tilesetName = FileUtilities.getNameWithoutExtention(mapurlFile);
File folderFile = new File(mapurlFile.getParentFile(), tilesetName);
mbtilesHelper = new MBTilesHelper();
mbtilesHelper.open(dbFile);
mbtilesHelper.createTables(false);
File[] zFolders = folderFile.listFiles();
List<File> xFolder = new ArrayList<File>();
for (File zFolder : zFolders) {
File[] xFiles = zFolder.listFiles();
for (File xFile : xFiles) {
if (xFile.isDirectory()) {
xFolder.add(xFile);
}
}
}
final GlobalMercator mercator = new GlobalMercator();
int minZ = 1000;
int maxZ = -1000;
EggClock clock = new EggClock("Time check: ", " sec");
clock.startAndPrint(System.out);
for (File xFile : xFolder) {
String zStr = xFile.getParentFile().getName();
final int z = Integer.parseInt(zStr);
minZ = min(minZ, z);
maxZ = max(maxZ, z);
String xStr = xFile.getName();
final int x = Integer.parseInt(xStr);
final File[] yFiles = xFile.listFiles(new FilenameFilter() {
public boolean accept(File arg0, String name) {
boolean endsWithPng = name.endsWith("png");
boolean endsWithJpg = name.endsWith("jpg");
if (endsWithPng || endsWithJpg) {
return true;
}
return false;
}
});
for (File yFile : yFiles) {
String yStr = FileUtilities.getNameWithoutExtention(yFile);
int y = Integer.parseInt(yStr);
Envelope wsen = mercator.TileLatLonBounds(x, y, z);
n = max(n, wsen.getMaxY());
e = max(e, wsen.getMaxX());
s = max(s, wsen.getMinY());
w = max(w, wsen.getMinX());
try {
BufferedImage image = ImageIO.read(yFile);
mbtilesHelper.addTile(x, y, z, image, format);
} catch (Exception e1) {
e1.printStackTrace();
}
if (imageIndex % 1000 == 0) {
pm.message("Images inserted in db: " + imageIndex);
clock.printTimePassedInSeconds(System.out);
}
imageIndex++;
}
}
mbtilesHelper.fillMetadata((float) n, (float) s, (float) w, (float) e, "tilesetName", format, minZ, maxZ);
mbtilesHelper.createIndexes();
mbtilesHelper.close();
}
use of oms3.annotations.In in project hortonmachine by TheHortonMachine.
the class OmsOnlineTilesDownloader method process.
@Execute
public void process() throws Exception {
checkNull(inPath, inServiceUrl, pEpsg, pMinzoom, pMaxzoom, pWest, pEast, pSouth, pNorth);
CoordinateReferenceSystem boundsCrs = CrsUtilities.getCrsFromEpsg(pEpsg, null);
CoordinateReferenceSystem mercatorCrs = CrsUtilities.getCrsFromEpsg(EPSG_MERCATOR, null);
CoordinateReferenceSystem latLongCrs = CrsUtilities.getCrsFromEpsg(EPSG_LATLONG, null);
ReferencedEnvelope inBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, boundsCrs);
MathTransform in2MercatorTransform = CRS.findMathTransform(boundsCrs, mercatorCrs);
Envelope mercatorEnvelope = JTS.transform(inBounds, in2MercatorTransform);
ReferencedEnvelope mercatorBounds = new ReferencedEnvelope(mercatorEnvelope, mercatorCrs);
MathTransform transform = CRS.findMathTransform(boundsCrs, latLongCrs);
Envelope latLongBounds = JTS.transform(inBounds, transform);
Coordinate latLongCentre = latLongBounds.centre();
File inFolder = new File(inPath);
File baseFolder = new File(inFolder, pName);
double w = latLongBounds.getMinX();
double s = latLongBounds.getMinY();
double e = latLongBounds.getMaxX();
double n = latLongBounds.getMaxY();
GlobalMercator mercator = new GlobalMercator();
for (int z = pMinzoom; z <= pMaxzoom; z++) {
// get ul and lr tile number in GOOGLE tiles
int[] llTileXY = mercator.GoogleTile(s, w, z);
int[] urTileXY = mercator.GoogleTile(n, e, z);
int startXTile = Math.min(llTileXY[0], urTileXY[0]);
int endXTile = Math.max(llTileXY[0], urTileXY[0]);
int startYTile = Math.min(llTileXY[1], urTileXY[1]);
int endYTile = Math.max(llTileXY[1], urTileXY[1]);
int tileNum = 0;
ReferencedEnvelope levelBounds = new ReferencedEnvelope();
pm.beginTask("Generating tiles at zoom level: " + z, (endXTile - startXTile + 1));
for (int i = startXTile; i <= endXTile; i++) {
for (int j = startYTile; j <= endYTile; j++) {
tileNum++;
Envelope bounds = mercator.TileLatLonBounds(i, j, z);
ReferencedEnvelope tmpBounds = new ReferencedEnvelope(bounds, latLongCrs);
levelBounds.expandToInclude(tmpBounds);
if (!doDryrun) {
int[] onlineTileNumbers = { i, j };
int[] fileNameTileNumbers = { i, j };
// switch( pType ) {
// case 1:
// need to convert in TMS format
int[] tmsNUms = mercator.TMSTileFromGoogleTile(i, j, z);
fileNameTileNumbers = tmsNUms;
// break;
// case 0:
// default:
// break;
// }
File imageFolder = new File(baseFolder, z + "/" + fileNameTileNumbers[0]);
if (!imageFolder.exists()) {
if (!imageFolder.mkdirs()) {
throw new ModelsIOException("Unable to create folder:" + imageFolder, this);
}
}
File imageFile = new File(imageFolder, fileNameTileNumbers[1] + ".png");
if (imageFile.exists()) {
continue;
}
String tmp = inServiceUrl.replaceFirst("ZZZ", String.valueOf(z));
tmp = tmp.replaceFirst("XXX", String.valueOf(onlineTileNumbers[0]));
tmp = tmp.replaceFirst("YYY", String.valueOf(onlineTileNumbers[1]));
// System.out.println(tmp);
URL url = new URL(tmp);
InputStream imgStream = null;
OutputStream out = null;
try {
imgStream = url.openStream();
out = new FileOutputStream(imageFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = imgStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
} catch (Exception ex) {
pm.errorMessage("Unable to get image: " + tmp);
} finally {
if (imgStream != null)
imgStream.close();
if (out != null) {
out.flush();
out.close();
}
}
}
}
pm.worked(1);
}
pm.done();
pm.message("Zoom level: " + z + " has " + tileNum + " tiles.");
pm.message("Boundary covered at Zoom level: " + z + ": " + levelBounds);
pm.message("Total boundary wanted: " + mercatorBounds);
}
StringBuilder properties = new StringBuilder();
properties.append("url=").append(pName).append("/ZZZ/XXX/YYY.png\n");
properties.append("minzoom=").append(pMinzoom).append("\n");
properties.append("maxzoom=").append(pMaxzoom).append("\n");
properties.append("center=").append(latLongCentre.x).append(" ").append(latLongCentre.y).append("\n");
properties.append("type=tms").append("\n");
File propFile = new File(inFolder, pName + ".mapurl");
FileUtilities.writeFile(properties.toString(), propFile);
}
use of oms3.annotations.In in project hortonmachine by TheHortonMachine.
the class OmsScanLineRasterizer method process.
@Execute
public void process() throws Exception {
checkNull(inVector);
if (pValue == null && fCat == null) {
throw new ModelsIllegalargumentException("One of pValue or the fCat have to be defined.", this, pm);
}
if (pNorth == null || pSouth == null || pWest == null || pEast == null || pRows == null || pCols == null) {
if (inRaster == null) {
throw new ModelsIllegalargumentException("It is necessary to supply all the information about the processing region. Did you set the boundaries and rows/cols?", this, pm);
}
}
if (inRaster != null) {
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
pNorth = regionMap.getNorth();
pSouth = regionMap.getSouth();
pWest = regionMap.getWest();
pEast = regionMap.getEast();
pRows = regionMap.getRows();
pCols = regionMap.getCols();
inIter = CoverageUtilities.getRandomIterator(inRaster);
}
SimpleFeatureType schema = inVector.getSchema();
CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
GridGeometry2D pGrid;
if (inRaster != null) {
pGrid = inRaster.getGridGeometry();
} else {
pGrid = gridGeometryFromRegionValues(pNorth, pSouth, pEast, pWest, pCols, pRows, crs);
}
if (outWR == null) {
paramsMap = gridGeometry2RegionParamsMap(pGrid);
height = paramsMap.getRows();
width = paramsMap.getCols();
xRes = paramsMap.getXres();
outWR = CoverageUtilities.createWritableRaster(width, height, null, null, doubleNovalue);
}
GeometryDescriptor geometryDescriptor = schema.getGeometryDescriptor();
if (EGeometryType.isPoint(geometryDescriptor)) {
throw new ModelsRuntimeException("Not implemented yet for points", this.getClass().getSimpleName());
} else if (EGeometryType.isLine(geometryDescriptor)) {
throw new ModelsRuntimeException("Not implemented yet for lines", this.getClass().getSimpleName());
} else if (EGeometryType.isPolygon(geometryDescriptor)) {
if (pUsePointInPolygon) {
if (inRaster == null) {
throw new ModelsIllegalargumentException("The point in polygon mode needs an input raster to work on.", this);
}
pm.beginTask("Prepare input data...", IHMProgressMonitor.UNKNOWN);
List<Geometry> allGeoms = FeatureUtilities.featureCollectionToGeometriesList(inVector, false, null);
Geometry allGeomsUnion = CascadedPolygonUnion.union(allGeoms);
PreparedGeometry preparedGeometry = PreparedGeometryFactory.prepare(allGeomsUnion);
pm.done();
double value = pValue;
pm.beginTask("Rasterizing...", height);
WritableRandomIter wIter = CoverageUtilities.getWritableRandomIterator(outWR);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
Coordinate coord = CoverageUtilities.coordinateFromColRow(col, row, pGrid);
if (preparedGeometry.intersects(gf.createPoint(coord))) {
wIter.setSample(col, col, 0, value);
}
}
pm.worked(1);
}
pm.done();
wIter.done();
} else {
rasterizepolygon(pGrid);
}
} else {
throw new ModelsIllegalargumentException("Couldn't recognize the geometry type of the file.", this.getClass().getSimpleName(), pm);
}
outRaster = CoverageUtilities.buildCoverage("rasterized", outWR, paramsMap, inVector.getSchema().getCoordinateReferenceSystem());
}
use of oms3.annotations.In in project hortonmachine by TheHortonMachine.
the class OmsVectorWriter method process.
// PARAM NAMES STOP
@Execute
public void process() throws Exception {
checkNull(file);
File vectorFile = new File(file);
if (inVector.size() == 0) {
pm.message("Warning, not writing an empty vector to file: " + vectorFile.getName());
return;
}
String name = vectorFile.getName();
if (name.toLowerCase().endsWith(HMConstants.SHP) || (pType != null && pType.equals(HMConstants.SHP))) {
if (vectorFile.exists() && !doOverwrite) {
throw new ModelsIOException("Overwriting is disabled. First delete the data.", this);
}
OmsShapefileFeatureWriter.writeShapefile(vectorFile.getAbsolutePath(), inVector, pm);
} else if (name.toLowerCase().contains("." + HMConstants.GPKG)) {
if (!name.contains(HMConstants.DB_TABLE_PATH_SEPARATOR)) {
throw new ModelsIllegalargumentException("The table name needs to be specified in the geopackage path after the #.", this);
}
String[] split = file.split(HMConstants.DB_TABLE_PATH_SEPARATOR);
if (split.length == 1 || split[1].trim().length() == 0) {
throw new ModelsIllegalargumentException("The geopackage contains several tables, the table neame needs to be specified in the path after the #.", this);
}
SqlName table = SqlName.m(split[1]);
String dbPath = split[0];
try (GeopackageCommonDb db = (GeopackageCommonDb) EDb.GEOPACKAGE.getSpatialDb()) {
boolean existed = db.open(dbPath);
db.initSpatialMetadata(null);
CoordinateReferenceSystem crs = inVector.getBounds().getCoordinateReferenceSystem();
int srid = CrsUtilities.getSrid(crs);
db.addCRS("EPSG", srid, crs.toWKT());
if (db.hasTable(table) && !doOverwrite) {
throw new ModelsIOException("Overwriting is disabled. First delete the data.", this);
}
if (!db.hasTable(table) || !existed) {
SpatialDbsImportUtils.createTableFromSchema(db, inVector.getSchema(), table, null, false);
}
SpatialDbsImportUtils.importFeatureCollection(db, inVector, table, -1, false, pm);
}
} else {
throw new IOException("Format is currently not supported for file: " + name);
}
}
use of oms3.annotations.In in project hortonmachine by TheHortonMachine.
the class OmsSigmaFilterPlus method process.
@Execute
public void process() throws Exception {
checkNull(inGeodata);
RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inGeodata);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double[] pixels = CoverageUtilities.renderedImage2DoubleArray(inGeodata.getRenderedImage(), 3);
/*
* Create a circular kernel of a given radius. Radius = 0.5 includes the 4 neighbors of the
* pixel in the center, radius = 1 corresponds to a 3x3 kernel size.
* The output is written to class variables kNPoints (number of points inside the kernel) and
* lineRadius, which is an array giving the radius of each line. Line length is 2*lineRadius+1.
*/
if (// this code creates the same sizes as the previous
pRadius >= 1.5 && pRadius < 1.75)
// RankFilters
pRadius = 1.75;
else if (pRadius >= 2.5 && pRadius < 2.85)
pRadius = 2.85;
int r2 = (int) (pRadius * pRadius) + 1;
kRadius = (int) (Math.sqrt(r2 + 1e-10));
lineRadius = new int[2 * kRadius + 1];
lineRadius[kRadius] = kRadius;
kNPoints = 2 * kRadius + 1;
for (int y = 1; y <= kRadius; y++) {
int dx = (int) (Math.sqrt(r2 - y * y + 1e-10));
lineRadius[kRadius + y] = dx;
lineRadius[kRadius - y] = dx;
kNPoints += 4 * dx + 2;
}
// process passes
for (int pass = 0; pass < nPasses; pass++) {
// min pixels in sigma
int minPixNumber = (int) (kNPoints * minPixFraction + 0.999999);
// range
pixels = doFiltering(pixels, cols, rows, kRadius, lineRadius, pSigmaWidth, minPixNumber, outlierAware);
}
WritableRaster outWR = CoverageUtilities.doubleArray2WritableRaster(pixels, cols, rows);
outGeodata = CoverageUtilities.buildCoverage("sigma", outWR, regionMap, inGeodata.getCoordinateReferenceSystem());
}
Aggregations