resolutionGroup.setText("output map resolution");
String res = "";
if (maps.size() > 0) {
try {
JGrassRegion activeWindow = maps.get(0).getActiveWindow();
res = String.valueOf(activeWindow.getNSResolution());
} catch (IOException e1) {
e1.printStackTrace();
}
}
Label xresLabel = new Label(resolutionGroup, SWT.NONE);
xresLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
xresLabel.setText("X resolution");
xresText = new Text(resolutionGroup, SWT.BORDER);
xresText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
| GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
xresText.setText(res);
Label yresLabel = new Label(resolutionGroup, SWT.NONE);
yresLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
yresLabel.setText("Y resolution");
yresText = new Text(resolutionGroup, SWT.BORDER);
yresText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
| GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
yresText.setText(res);
return parent;
}
protected void okPressed() {
locationName = locNameText.getText();
if (locationName == null || locationName.length() < 1) {
locationName = "newLocation";
}
mapsetName = mapsetNameText.getText();
if (mapsetName == null || mapsetName.length() < 1) {
mapsetName = "newMapset";
}
xRes = xresText.getText();
yRes = yresText.getText();
Object crsData = crsText.getData();
if (crsData instanceof CoordinateReferenceSystem) {
crs = (CoordinateReferenceSystem) crsData;
}
super.okPressed();
}
};
dialog.setBlockOnOpen(true);
open = dialog.open();
} catch (Exception e) {
String message = "An error occurred while exporting the maps.";
ExceptionDetailsDialog.openError("ERROR", message, IStatus.ERROR, JGrassPlugin.PLUGIN_ID, e);
}
}
});
if (open == SWT.CANCEL) {
return;
}
/*
* run with backgroundable progress monitoring
*/
IRunnableWithProgress operation = new IRunnableWithProgress(){
public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
if (locationName == null || mapsetName == null || crs == null || maps.size() < 1) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR);
msgBox.setMessage("An error occurred in processing the user supplied data.");
msgBox.open();
return;
}
/*
* finally do some processing
*/
int mapNum = maps.size();
monitor.beginTask("Reprojecting maps to new Location...", mapNum);
for( int i = 0; i < mapNum; i++ ) {
JGrassMapGeoResource tmpMap = maps.get(i);
JGrassMapEnvironment jgMEnv = new JGrassMapEnvironment(tmpMap.getMapFile());
try {
/*
* TODO get envelope from original region and reproject it.
* then adapt it to the output resolution and
* finally reproject the map on the new grid.
*/
JGrassRegion sourceRegion = jgMEnv.getActiveRegion();
CoordinateReferenceSystem sourceCrs = jgMEnv.getCoordinateReferenceSystem();
com.vividsolutions.jts.geom.Envelope sourceEnvelope = sourceRegion.getEnvelope();
MathTransform tr = CRS.findMathTransform(sourceCrs, crs);
com.vividsolutions.jts.geom.Envelope outputEnvelope = JTS.transform(sourceEnvelope, tr);
double west = outputEnvelope.getMinX();
double east = outputEnvelope.getMaxX();
double south = outputEnvelope.getMinY();
double north = outputEnvelope.getMaxY();
double xResolution = Double.parseDouble(xRes);
double yResolution = Double.parseDouble(yRes);
Coordinate tmp1 = new Coordinate(west, south);
Coordinate tmp2 = new Coordinate(west + xResolution, south + yResolution);
JTS.transform(tmp1, tmp1, tr);
JTS.transform(tmp2, tmp2, tr);
xResolution = tmp2.x - tmp1.x;
yResolution = tmp2.y - tmp1.y;
// if in metric, snap to integer bounds
int intWest = (int) Math.floor(west);
int intSouth = (int) Math.floor(south);
if (west - intWest < xResolution && south - intSouth < yResolution) {
west = intWest;
south = intSouth;
}
double w = east - west;
double h = north - south;
// modify the envelope to be in the requested resolution
double cols = Math.floor(w / xResolution) + 1.0;
double rows = Math.floor(h / yResolution) + 1.0;
double newEast = west + cols * xResolution;
double newNorth = south + rows * yResolution;
ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(west, newEast, south, newNorth, crs);
GridToEnvelopeMapper g2eMapper = new GridToEnvelopeMapper();
g2eMapper.setEnvelope(referencedEnvelope);
GridEnvelope2D gridEnvelope2D = new GridEnvelope2D(0, 0, (int) cols, (int) rows);
g2eMapper.setGridRange(gridEnvelope2D);
g2eMapper.setPixelAnchor(PixelInCell.CELL_CENTER);
MathTransform gridToEnvelopeTransform = g2eMapper.createTransform();
GridGeometry outputGridGeometry = new GridGeometry2D(gridEnvelope2D, gridToEnvelopeTransform, crs);
GridCoverage2D coverage2D = JGrassCatalogUtilities.getGridcoverageFromGrassraster(jgMEnv, sourceRegion);
// GrassCoverageReadParam gcReadParam = new
// GrassCoverageReadParam(sourceRegion);
GridCoverage2D reprojected = (GridCoverage2D) Operations.DEFAULT.resample(coverage2D, crs,
outputGridGeometry, Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
JGrassRegion jgRegion = new JGrassRegion(west, newEast, south, newNorth, xResolution, yResolution);
// GridCoverage2D coverage2D = tmp.read(null);
// GridCoverage2D reprojected = (GridCoverage2D)
// Operations.DEFAULT.resample(
// coverage2D, crs);