Reading ASTER DEM Data

3 messages Options
Embed this post
Permalink
Ahmad Al-Obaidy

Reading ASTER DEM Data

Reply Threaded More More options
Print post
Permalink
Hi,

How can I read ASTER DEM data set. I have folder that contains tiles of ASTER in tif format, and I want to write a function that get lon/lat as arguments and return a number as an elevation.

I wrote a class to do that, but I think I have something wrong.

import java.util.concurrent.ConcurrentHashMap;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.iterator.RandomIter;
import javax.media.jai.iterator.RandomIterFactory;

public class AsterDemReader implements DemProvider {

    // base path for the tiles
    private String basePath;

    // tiles RAM cache
    private ConcurrentHashMap<String, RandomIter> cache = new ConcurrentHashMap<String, RandomIter>();

    public AsterDemReader(String basePath) {
        this.basePath = basePath;
    }

    private RandomIter getTile(String tileName) {
        RandomIter iter = cache.get(tileName);
        if (iter == null) {
            PlanarImage image = JAI.create("fileload", String.format(
                    "%s/ASTGTM_%s_dem.tif", basePath, tileName));
            iter = RandomIterFactory.create(image, null);
            cache.put(tileName, iter);
        }
        return iter;
    }

    // lat and lon in decimal degrees
    /*
     * (non-Javadoc)
     *
     * @see com.obteq.aster.reader.DemProvider#getElevation(double, double)
     */
    public double getElevation(double lat, double lon) throws Exception {

        lat -= .0001389;
        lon -= .0001389;

        // file name
        int latDegree = (int) Math.abs(lat);
        int lonDegree = (int) Math.abs(lon);

        char northSouth = (lat < 0) ? 'E' : 'N';
        char eastWest = (lon < 0) ? 'W' : 'E';

        String tileName = String.format("%c%2d%c%3d", northSouth, latDegree,
                eastWest, lonDegree).replace(" ", "0");
        // the frection only
        lat -= (int) lat;
        lon -= (int) lon;

        lat = Math.abs(lat);
        lon = Math.abs(lon);

        // calculate x and y of the image
        int y = 3600 - (int) (3600 * lat);
        int x = (int) (3600 * lon);
        // System.out.println(String.format("x=%d y=%d", x, y));

        // get the image from cache

        RandomIter iter = getTile(tileName);
        double av = iter.getSampleDouble(x, y, 0);
       
        return av;
    }

    public static void main(String args[]) throws Exception {
        DemProvider aster = new AsterDemReader("D:/OldHP");
        double elev = aster.getElevation(33.5, 42.5);

        System.out.println(elev);
    }
}

Could anyone please point me where I'm getting wrong? where can I find more info about reading ASTER DEM?

Best Regards,


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Reading ASTER DEM Data

Reply Threaded More More options
Print post
Permalink
Hi Ahmad,

I think the problem is the JAI "fileload" operation which is notorious for being unable to handle many tif formats.  But also, you are doing a lot of work that GeoTools can do for you.

Here is an example...

        File file = ...

        GeoTiffReader reader = new GeoTiffReader(file);
        GridCoverage2D cov = (GridCoverage2D) reader.read(null);

        GridEnvelope gridBounds = cov.getGridGeometry().getGridRange();
        System.out.println("grid bounds: " + gridBounds);

        Envelope2D worldBounds = cov.getEnvelope2D();
        System.out.println("world bounds: " + worldBounds);

        int numBands = cov.getNumSampleDimensions();
        System.out.println("num bands: " + numBands);

        CoordinateReferenceSystem crs = cov.getCoordinateReferenceSystem();

        // get the value of the centre location as an example
        double[] values = new double[numBands];
        double lon = worldBounds.getCenterX();
        double lat = worldBounds.getCenterY();
        DirectPosition dp = new DirectPosition2D(crs, lon, lat);
        cov.evaluate(dp, values);

        System.out.println("value at " + dp + " is " + values[0]);

As well as this you can still work directly with the underlying image using JAI iterators if you want to...

        RenderedImage image = cov.getRenderedImage();

If you are using GeoTools 2.5.x you can convert between world coordinates and grid coordinates like this:
        MathTransform2D worldToGrid = cov.getGridGeometry().getCRSToGrid2D();
        Point2D point = new Point2D.Double(lon, lat);
        Point2D gridPoint = worldToGrid.transform(point, null);

If you are using GeoTools 2.6-M1 you can do this as well...
        GridCoordinates2D gridCoords = cov.getGridGeometry().worldToGrid(pos);

Hope this helps,
Michael


2009/9/28 Ahmad Al-Obaidy <[hidden email]>
Hi,

How can I read ASTER DEM data set. I have folder that contains tiles of ASTER in tif format, and I want to write a function that get lon/lat as arguments and return a number as an elevation.

I wrote a class to do that, but I think I have something wrong.

import java.util.concurrent.ConcurrentHashMap;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.iterator.RandomIter;
import javax.media.jai.iterator.RandomIterFactory;

public class AsterDemReader implements DemProvider {

    // base path for the tiles
    private String basePath;

    // tiles RAM cache
    private ConcurrentHashMap<String, RandomIter> cache = new ConcurrentHashMap<String, RandomIter>();

    public AsterDemReader(String basePath) {
        this.basePath = basePath;
    }

    private RandomIter getTile(String tileName) {
        RandomIter iter = cache.get(tileName);
        if (iter == null) {
            PlanarImage image = JAI.create("fileload", String.format(
                    "%s/ASTGTM_%s_dem.tif", basePath, tileName));
            iter = RandomIterFactory.create(image, null);
            cache.put(tileName, iter);
        }
        return iter;
    }

    // lat and lon in decimal degrees
    /*
     * (non-Javadoc)
     *
     * @see com.obteq.aster.reader.DemProvider#getElevation(double, double)
     */
    public double getElevation(double lat, double lon) throws Exception {

        lat -= .0001389;
        lon -= .0001389;

        // file name
        int latDegree = (int) Math.abs(lat);
        int lonDegree = (int) Math.abs(lon);

        char northSouth = (lat < 0) ? 'E' : 'N';
        char eastWest = (lon < 0) ? 'W' : 'E';

        String tileName = String.format("%c%2d%c%3d", northSouth, latDegree,
                eastWest, lonDegree).replace(" ", "0");
        // the frection only
        lat -= (int) lat;
        lon -= (int) lon;

        lat = Math.abs(lat);
        lon = Math.abs(lon);

        // calculate x and y of the image
        int y = 3600 - (int) (3600 * lat);
        int x = (int) (3600 * lon);
        // System.out.println(String.format("x=%d y=%d", x, y));

        // get the image from cache

        RandomIter iter = getTile(tileName);
        double av = iter.getSampleDouble(x, y, 0);
       
        return av;
    }

    public static void main(String args[]) throws Exception {
        DemProvider aster = new AsterDemReader("D:/OldHP");
        double elev = aster.getElevation(33.5, 42.5);

        System.out.println(elev);
    }
}

Could anyone please point me where I'm getting wrong? where can I find more info about reading ASTER DEM?

Best Regards,


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Reading ASTER DEM Data

Reply Threaded More More options
Print post
Permalink
PS. I forgot to include the imports for that code example...

import java.awt.geom.Point2D;
import java.io.File;
import org.geotools.coverage.grid.GridCoordinates2D;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.Envelope2D;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.coverage.grid.GridEnvelope;
import org.opengis.geometry.DirectPosition;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform2D;




------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users