Java Code Examples for org.gdal.gdal.Driver

The following are top voted examples for showing how to use org.gdal.gdal.Driver. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: faims-android   File: GdalMapLayer.java   Source Code and License 5 votes vote down vote up
private Dataset reprojectDataset(Dataset data, double pixelSpacing, SpatialReference fromProj,
            SpatialReference toProjection) {

        double[] adfGeoTransform = new double[6];
        data.GetGeoTransform(adfGeoTransform);
        
        Envelope newBounds = bounds(data,toProjection);
        Driver memoryDriver = gdal.GetDriverByName("MEM");
        int w = (int)((newBounds.getMaxX() - newBounds.getMinX())/pixelSpacing);
        int h =  (int)((newBounds.getMaxY() - newBounds.getMinY())/pixelSpacing);
                
        Dataset dest = memoryDriver.Create("", w, h, data.getRasterCount(), gdalconst.GDT_Byte);
//        double[] newGeotransform = {newBounds.getMinX(), pixelSpacing, adfGeoTransform[2], newBounds.getMaxY() , adfGeoTransform[4], -pixelSpacing };
        double[] newGeotransform = {-9662887.997233687,9.269968462003103,0,3574568.743162234,0,-9.269968462003103};
        dest.SetGeoTransform( newGeotransform );
        dest.SetProjection ( toProjection.ExportToWkt() );
        Log.debug("start reprojection");
        long time = System.currentTimeMillis();
        int res = gdal.ReprojectImage( data, dest, 
                toProjection.ExportToWkt(), fromProj.ExportToWkt(),
                gdalconst.GRA_NearestNeighbour, 6.71089e+07, 0.125 ,new ProgressCallback(){
            @Override
            public int run(double dfComplete, String message)
            {
                Log.debug("Progress: "+dfComplete+" msg:"+message);
                return 0;
            }
        });
        long timeTook =  System.currentTimeMillis()-time;
        Log.debug("projection res = " + res + " time ms: "+timeTook);
        if(res == gdalconst.CE_Failure){
            Log.error("error in reprojecting: "+gdal.GetLastErrorMsg());
        }
        dest.GetRasterBand(1).SetColorInterpretation(data.GetRasterBand(1).GetColorInterpretation());
        dest.GetRasterBand(1).SetColorTable(data.GetRasterBand(1).GetColorTable());
        return dest;
    }
 
Example 2
Project: faims-android   File: GdalMapLayer.java   Source Code and License 5 votes vote down vote up
private void listDrivers() {
    for (int i=0;i<gdal.GetDriverCount();i++){
        Driver driver = gdal.GetDriver(i);
        Hashtable metaData = driver.GetMetadata_Dict();
        Log.info("driver " + driver.getShortName()+" ("+driver.getLongName()+")");
        Log.info("extension: " + metaData.get("DMD_EXTENSION"));
        Log.info(Arrays.toString(driver.GetMetadata_Dict().entrySet().toArray()));
    }
}
 
Example 3
Project: s2tbx   File: GDALProductReader.java   Source Code and License 5 votes vote down vote up
private static MetadataElement buildMetadataElement(Dataset gdalProduct) {
    Driver hDriver = gdalProduct.GetDriver();
    int imageWidth = gdalProduct.getRasterXSize();
    int imageHeight = gdalProduct.getRasterYSize();
    MetadataElement metadataElement = new MetadataElement("Image info");
    metadataElement.setAttributeString("driver", hDriver.getShortName());
    metadataElement.setAttributeInt("width", imageWidth);
    metadataElement.setAttributeInt("height", imageHeight);

    double[] adfGeoTransform = new double[6];
    gdalProduct.GetGeoTransform(adfGeoTransform);
    double originX = adfGeoTransform[0];
    double originY = adfGeoTransform[3];
    double pixelSizeX = adfGeoTransform[1];
    double pixelSizeY = (adfGeoTransform[5] > 0) ? adfGeoTransform[5] : -adfGeoTransform[5];

    if (adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0) {
        metadataElement.setAttributeString("origin", originX + "x" + originY);
        metadataElement.setAttributeString("pixel size", pixelSizeX + "x" + pixelSizeY);
    } else {
        String str1 = adfGeoTransform[0] + "," + adfGeoTransform[1]+ "," + adfGeoTransform[3];
        String str2 = adfGeoTransform[3] + "," + adfGeoTransform[4]+ "," + adfGeoTransform[5];
        metadataElement.setAttributeString("geo transform", str1 + " " + str2);
    }

    Hashtable<?, ?> dict = gdalProduct.GetMetadata_Dict("");
    Enumeration keys = dict.keys();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        String value = (String)dict.get(key);
        if (!StringUtils.isNullOrEmpty(key) && !StringUtils.isNullOrEmpty(value)) {
            metadataElement.setAttributeString(key, value);
        }
    }
    return metadataElement;
}