use of org.gdal.osr.SpatialReference in project imageio-ext by geosolutions-it.
the class GDALImageReader method read.
/**
* Read the raster and returns a <code>BufferedImage</code>
*
* @param imageIndex
* the index of the image to be retrieved.
* @param param
* an <code>ImageReadParam</code> used to control the
* reading process, or <code>null</code>. Actually,
* setting a destinationType allows to specify the number of
* bands in the destination image.
*
* @return the desired portion of the image as a <code>BufferedImage</code>
* @throws IllegalArgumentException
* if <code>param</code> contains an invalid specification
* of a source and/or destination band subset or of a
* destination image.
* @throws IOException
* if an error occurs when acquiring access to the
* underlying datasource
*/
public BufferedImage read(final int imageIndex, final ImageReadParam param) throws IOException {
// //
//
// Retrieving the requested dataset
//
// //
final GDALCommonIIOImageMetadata item = getDatasetMetadata(imageIndex);
int width = item.getWidth();
int height = item.getHeight();
final Dataset originalDataset = datasetsMap.get(item.getDatasetName());
if (originalDataset == null)
throw new IOException("Error while acquiring the input dataset " + item.getDatasetName());
Dataset dataset = originalDataset;
Dataset warpedDataset = null;
try {
if (param instanceof GDALImageReadParam) {
GDALImageReadParam gdalImageReadParam = (GDALImageReadParam) param;
// Check for source != destination
String sourceWkt = dataset.GetProjection();
String destinationWkt = gdalImageReadParam.getDestinationWkt();
SpatialReference sourceReference = new SpatialReference(sourceWkt);
SpatialReference destinationReference = new SpatialReference(destinationWkt);
// lets warp the image using GDAL.
if (sourceReference.IsSame(destinationReference) == 0) {
dataset = gdal.AutoCreateWarpedVRT(dataset, dataset.GetProjection(), destinationWkt, gdalImageReadParam.getResampleAlgorithm().getGDALResampleAlgorithm(), gdalImageReadParam.getMaxError());
warpedDataset = dataset;
// width and height may have changed from original metadata
width = warpedDataset.getRasterXSize();
height = warpedDataset.getRasterYSize();
}
}
final SampleModel itemSampleModel = item.getSampleModel();
int itemNBands = itemSampleModel.getNumBands();
int nDestBands;
BufferedImage bi = null;
final ImageReadParam imageReadParam;
if (param == null)
imageReadParam = getDefaultReadParam();
else
imageReadParam = param;
// //
//
// First, check for a specified ImageTypeSpecifier
//
// //
ImageTypeSpecifier imageType = imageReadParam.getDestinationType();
SampleModel destSampleModel = null;
if (imageType != null) {
destSampleModel = imageType.getSampleModel();
nDestBands = destSampleModel.getNumBands();
} else {
bi = imageReadParam.getDestination();
if (bi != null)
nDestBands = bi.getSampleModel().getNumBands();
else
nDestBands = itemNBands;
}
// //
//
// Second, bands settings check
//
// //
checkReadParamBandSettings(imageReadParam, itemNBands, nDestBands);
int[] srcBands = imageReadParam.getSourceBands();
// int[] destBands = imageReadParam.getDestinationBands();
//
// //
//
// Third, destination image check
//
// //
// if (bi != null && imageType == null) {
// if ((srcBands == null) && (destBands == null)) {
// SampleModel biSampleModel = bi.getSampleModel();
// if (!bi.getColorModel().equals(item.getColorModel())
// || biSampleModel.getDataType() != itemSampleModel
// .getDataType())
// throw new IllegalArgumentException(
// "Provided destination image does not have a valid ColorModel or
// SampleModel");
// }
// }
// //
//
// Computing regions of interest
//
// //
Rectangle srcRegion = new Rectangle(0, 0, 0, 0);
Rectangle destRegion = new Rectangle(0, 0, 0, 0);
computeRegions(imageReadParam, width, height, bi, srcRegion, destRegion);
if (imageReadParam != null) {
if (imageReadParam instanceof EnhancedImageReadParam) {
final EnhancedImageReadParam eparam = (EnhancedImageReadParam) imageReadParam;
final Rectangle dstRegion = eparam.getDestinationRegion();
if (dstRegion != null) {
destRegion.height = dstRegion.height;
destRegion.width = dstRegion.width;
}
}
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Source Region = " + srcRegion.toString());
LOGGER.fine("Destination Region = " + destRegion.toString());
}
//
if (bi == null) {
// //
//
// No destination image has been specified.
// Creating a new BufferedImage
//
// //
ColorModel cm;
if (imageType == null) {
cm = item.getColorModel();
bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands), false, null);
} else {
cm = imageType.getColorModel();
bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(destSampleModel, dataset, srcRegion, destRegion, srcBands), false, null);
}
} else {
// //
//
// the destination image has been specified.
//
// //
// Rectangle destSize = (Rectangle) destRegion.clone();
// destSize.setLocation(0, 0);
Raster readRaster = readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands);
WritableRaster raster = bi.getRaster().createWritableChild(0, 0, bi.getWidth(), bi.getHeight(), 0, 0, null);
// TODO: Work directly on a Databuffer avoiding setRect?
raster.setRect(destRegion.x, destRegion.y, readRaster);
// Raster readRaster = readDatasetRaster(item, srcRegion,
// destRegion,
// srcBands);
// WritableRaster raster = bi.getRaster().createWritableChild(
// destRegion.x, destRegion.y, destRegion.width,
// destRegion.height, destRegion.x, destRegion.y, null);
// //TODO: Work directly on a Databuffer avoiding setRect?
// raster.setRect(readRaster);
}
return bi;
} finally {
if (warpedDataset != null) {
GDALUtilities.closeDataSet(warpedDataset);
}
}
}
use of org.gdal.osr.SpatialReference in project com.revolsys.open by revolsys.
the class OgrRecordStore method newLayerRecordDefinition.
protected RecordDefinitionImpl newLayerRecordDefinition(final RecordStoreSchema schema, final Layer layer) {
final String layerName = layer.GetName();
final PathName typePath = PathName.newPathName(layerName);
/**
* This primes the layer so that the fidColumn is loaded correctly.
*/
layer.GetNextFeature();
final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
String idFieldName = layer.GetFIDColumn();
if (!Property.hasValue(idFieldName)) {
idFieldName = "rowid";
}
this.idFieldNames.put(typePath.getUpperPath(), idFieldName);
final FeatureDefn layerDefinition = layer.GetLayerDefn();
if (SQLITE.equals(this.driverName) || GEO_PAKCAGE.equals(this.driverName)) {
recordDefinition.addField(idFieldName, DataTypes.LONG, true);
recordDefinition.setIdFieldName(idFieldName);
}
for (int fieldIndex = 0; fieldIndex < layerDefinition.GetFieldCount(); fieldIndex++) {
final FieldDefn fieldDefinition = layerDefinition.GetFieldDefn(fieldIndex);
final String fieldName = fieldDefinition.GetName();
final int fieldType = fieldDefinition.GetFieldType();
final int fieldWidth = fieldDefinition.GetWidth();
final int fieldPrecision = fieldDefinition.GetPrecision();
DataType fieldDataType;
switch(fieldType) {
case 0:
fieldDataType = DataTypes.INT;
break;
case 2:
fieldDataType = DataTypes.DOUBLE;
break;
case 4:
case 6:
fieldDataType = DataTypes.STRING;
break;
case 9:
fieldDataType = DataTypes.DATE;
break;
case 11:
fieldDataType = DataTypes.DATE_TIME;
break;
default:
fieldDataType = DataTypes.STRING;
final String fieldTypeName = fieldDefinition.GetFieldTypeName(fieldType);
Logs.error(this, "Unsupported field type " + this.file + " " + fieldName + ": " + fieldTypeName);
break;
}
final FieldDefinition field = new FieldDefinition(fieldName, fieldDataType, fieldWidth, fieldPrecision, false);
recordDefinition.addField(field);
}
for (int fieldIndex = 0; fieldIndex < layerDefinition.GetGeomFieldCount(); fieldIndex++) {
final GeomFieldDefn fieldDefinition = layerDefinition.GetGeomFieldDefn(fieldIndex);
final String fieldName = fieldDefinition.GetName();
final int geometryFieldType = fieldDefinition.GetFieldType();
DataType geometryFieldDataType;
int axisCount = 2;
switch(geometryFieldType) {
case 1:
geometryFieldDataType = DataTypes.POINT;
break;
case 2:
geometryFieldDataType = DataTypes.LINE_STRING;
break;
case 3:
geometryFieldDataType = DataTypes.POLYGON;
break;
case 4:
geometryFieldDataType = DataTypes.MULTI_POINT;
break;
case 5:
geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
break;
case 6:
geometryFieldDataType = DataTypes.MULTI_POLYGON;
break;
case 7:
geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
break;
case 101:
geometryFieldDataType = DataTypes.LINEAR_RING;
break;
case 0x80000000 + 1:
geometryFieldDataType = DataTypes.POINT;
axisCount = 3;
break;
case 0x80000000 + 2:
geometryFieldDataType = DataTypes.LINE_STRING;
axisCount = 3;
break;
case 0x80000000 + 3:
geometryFieldDataType = DataTypes.POLYGON;
axisCount = 3;
break;
case 0x80000000 + 4:
geometryFieldDataType = DataTypes.MULTI_POINT;
axisCount = 3;
break;
case 0x80000000 + 5:
geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
axisCount = 3;
break;
case 0x80000000 + 6:
geometryFieldDataType = DataTypes.MULTI_POLYGON;
axisCount = 3;
break;
case 0x80000000 + 7:
geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
axisCount = 3;
break;
default:
geometryFieldDataType = DataTypes.GEOMETRY;
break;
}
final SpatialReference spatialReference = fieldDefinition.GetSpatialRef();
final GeometryFactory geometryFactory = Gdal.getGeometryFactory(spatialReference, axisCount);
final FieldDefinition field = new FieldDefinition(fieldName, geometryFieldDataType, false);
field.setProperty(FieldProperties.GEOMETRY_FACTORY, geometryFactory);
recordDefinition.addField(field);
}
return recordDefinition;
}
use of org.gdal.osr.SpatialReference in project com.revolsys.open by revolsys.
the class OgrRecordStore method newLayerRecordDefinition.
private RecordDefinition newLayerRecordDefinition(final DataSource dataSource, final RecordDefinition sourceRecordDefinition) {
final PathName typePath = sourceRecordDefinition.getPathName();
final String name = typePath.getName();
final PathName parentPath = typePath.getParent();
final RecordStoreSchema schema = getSchema(parentPath);
Layer layer;
if (sourceRecordDefinition.hasGeometryField()) {
final GeometryFactory geometryFactory = sourceRecordDefinition.getGeometryFactory();
final FieldDefinition geometryField = sourceRecordDefinition.getGeometryField();
final int geometryFieldType = getGeometryFieldType(geometryFactory, geometryField);
final SpatialReference spatialReference = Gdal.getSpatialReference(geometryFactory);
layer = dataSource.CreateLayer(typePath.getPath(), spatialReference, geometryFieldType);
} else {
layer = dataSource.CreateLayer(name);
}
if (dataSource.TestCapability(ogrConstants.ODsCCreateLayer) == false) {
System.err.println("CreateLayer not supported by driver.");
}
return newLayerRecordDefinition(schema, layer);
}
use of org.gdal.osr.SpatialReference in project imageio-ext by geosolutions-it.
the class GeoTiffTest method readWithWarp.
/**
* Test Read exploiting JAI-ImageIO tools capabilities
* and GDAL warp.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void readWithWarp() throws FileNotFoundException, IOException {
if (!isGDALAvailable) {
return;
}
final ParameterBlockJAI pbjImageRead;
String fileName = "utmByte.tif";
final File file = TestData.file(this, fileName);
SpatialReference destinationReference = new SpatialReference();
destinationReference.SetProjCS("UTM 17 (WGS84) in northern hemisphere.");
destinationReference.SetWellKnownGeogCS("WGS84");
destinationReference.SetUTM(17, 1);
GDALImageReadParam readParam = new GDALImageReadParam();
readParam.setDestinationWkt(destinationReference.ExportToWkt());
readParam.setResampleAlgorithm(ResampleAlgorithm.CUBIC);
pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", new FileImageInputStreamExtImpl(file));
pbjImageRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
pbjImageRead.setParameter("ReadParam", readParam);
RenderedOp image = JAI.create("ImageRead", pbjImageRead);
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "", true);
else
Assert.assertNotNull(image.getTiles());
}
use of org.gdal.osr.SpatialReference in project com.revolsys.open by revolsys.
the class Gdal method getSpatialReference.
public static SpatialReference getSpatialReference(final int srid) {
final SpatialReference spatialReference = new SpatialReference("");
spatialReference.ImportFromEPSG(srid);
return spatialReference;
}
Aggregations