RE: [OpenMap Users] GeoTiff how to ??

From: Chris Allport <callport_at_dtiweb.net>
Date: Wed, 23 Apr 2008 07:46:28 -0400

Carsten posted some code a while back, here is the link:

 

http://openmap.bbn.com/mailArchives/openmap-users/2005-10/4353.html

 

The following is the code that I use to install the layer:

 

JAIPlugIn jpi = new JAIPlugIn();

 

jpi.loadRaster(global.pathToGeoTiffData);

 

PlugInLayer geoTiffLayer = new PlugInLayer();

 

geoTiffLayer.setPlugIn(jpi);

 

mapHandler.add(geoTiffLayer);

 

-Chris

 

 

________________________________

From: owner-openmap-users_at_bbn.com [mailto:owner-openmap-users_at_bbn.com]
On Behalf Of Ta3ban4_at_aol.com
Sent: Tuesday, April 22, 2008 3:45 PM
To: openmap-users_at_bbn.com
Subject: [OpenMap Users] GeoTiff how to ??

 

Hi All, I'm newbie with openmap API . I trying to display geotiff files
and i did find the following class JAIPlugIn shown below from openmap
mailing list Archive . Unfortunately I couldn't get it to display the

geotiff file . Any help would be really appreciated.

 

Thanks

Mike

 

the following is how i was setting it up

 

Properties jaiProps = new Properties();
jaiProps.put("jailayer.prettyName",
repro.getProperty("jailayer.prettyName"));
jaiProps.put("jailayer.imageFile",
repro.getProperty("jailayer.imageFile"));
 jai.setProperties("jailayer", jaiProps);
PlugInLayer pl = new PlugInLayer();
pl.setPlugIn(jai);
pl.setVisible(true);

layerHandler.addLayer(pl);

 

//*********************************************///

import java.awt.Component;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

 

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

 

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMScalingRaster;
import com.bbn.openmap.plugin.OMGraphicHandlerPlugIn;
import com.bbn.openmap.proj.Projection;
import com.bbn.openmap.proj.coords.DMSLatLonPoint;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.util.PropUtils;

 

import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDirectory;
import com.sun.media.jai.codec.TIFFField;
/**
 *
 * _at_author mahmoudm
 */
public class JAIPlugIn extends OMGraphicHandlerPlugIn
{

 

    double lat, lon, lat2, lon2, ximage_scale, yimage_scale;

 

    // sourcelat,sourcelon;

 

    protected PlanarImage source = null;

 

    boolean DEBUG = true;

 

    /** The property for the data file - tileFile. */
    public final static String FileNameProperty = "imageFile";

 

    /**
     * The property for whether the data file has a descriptive header
        on the
     * first line, to let the reader know to ignore that line -
        fileHasHeader.
     * Default is true.
     */

 

    protected String imageFileName = null;

 

    // the map
    OMScalingRaster raster = null;

 

    /**
     * Default constructor.
     */
    public JAIPlugIn()
    {
        super();
        DEBUG = Debug.debugging("JAIPlugIn");
    }

 

    public JAIPlugIn(Component comp)
    {
        super(comp);
    }

 

    /**
     * The getRectangle call is the main call into the PlugIn module.
     * The module
     * is expected to fill the graphics list with objects that are
     * within the
     * screen parameters passed.
     *
     * _at_param p
     * projection of the screen, holding scale, center coords,
     * height, width.
     */
    public OMGraphicList getRectangle(Projection p)
    {

 

        OMGraphicList list = (OMGraphicList) getList();
        list.clear();

 

        if (DEBUG)
        {
            Debug.output("CSVTIPI: getRectangle");
        }

 

        if (raster == null)
        {
            loadRaster(imageFileName);
        }

 

        if (raster.isOnMap(p))
        {
            if (DEBUG) {
                Debug.output("CSVTIPI: image on map");
            }
            raster.generate(p);
            list.add(raster);
        }
        else if (DEBUG)
        {
            Debug.output("CSVTIPI: image not on map, skipping");
        }

 

        repaint();
        return list;

 

    } // end getRectangle

 

    /**
     * PropertyConsumer method, setting the PlugIn with properties that
     * apply to
     * it.
     */
    public void setProperties(String prefix, Properties props)
    {
        super.setProperties(prefix, props);

 

        String realPrefix = PropUtils.getScopedPropertyPrefix(prefix);

 

        imageFileName = props.getProperty(realPrefix +
FileNameProperty);

 

        if (DEBUG)
        {
            Debug.output("CSVTIPI: file: " + imageFileName);
        }

 

    }

 

    /**
     * Takes the URL to a csv file and parses it into OMScaledRasters,
     * adding them to the tiles HashSet.
     */
    protected void loadRaster(String csvFileName)
    {
        if (imageFileName != null)
        {
            try
            {
                init();
            } catch (FileNotFoundException e)
            {
                //Debug.output("CSVTIPI: file: " + imageFileName + " not
found");
                e.printStackTrace();
            }
            // OMScalingRaster omsr
            raster = new OMScalingRaster((float) lat, (float) lon,
                    (float) lat2, (float) lon2,
            source.getAsBufferedImage());
        }
    }

 

    /**
     * Method to fill in a Properties object, reflecting the current
     * values of
     * the PropertyConsumer. If the PropertyConsumer has a prefix set,
the
     * property keys should have that prefix plus a separating '.'
     * prepended to
     * each propery key it uses for configuration.
     *
     * _at_param getList
     * a Properties object to load the PropertyConsumer properties
     * into. If getList equals null, then a new Properties object
     * should be created.
     * _at_return Properties object containing PropertyConsumer property
values. If
     * getList was not null, this should equal getList.Otherwise, it
     * should be the Properties object created by the
     * PropertyConsumer.
     */
    public Properties getProperties(Properties getList) {
        if (getList == null) {
            getList = new Properties();
        }

 

        String prefix = PropUtils.getScopedPropertyPrefix(this);

 

        getList.put(prefix + FileNameProperty,
PropUtils.unnull(imageFileName));

 

        return getList;
    }

 

    int sourceWidth;

 

    int sourceHeigth;

 

    public void init() throws FileNotFoundException {
        try {

 

            String associatedfile = imageFileName;
            source = JAI.create("fileload", associatedfile);

 

            sourceWidth = source.getWidth();
            sourceHeigth = source.getHeight();

 

            double xscale, yscale;
            double lonNW, latNW;
            String projfilename = imageFileName;
            FileSeekableStream st;

 

            if (imageFileName.endsWith(".tif")
                    || imageFileName.endsWith(".TIF")) {

 

                st = new FileSeekableStream(imageFileName);

 

                TIFFDirectory td;
                td = new TIFFDirectory(st, 0);

 

                TIFFField tfMPS = td.getField(33550); //
ModelPixelScaleTag
                TIFFField tfMTP = td.getField(33922); //
ModelTiepointTag

 

                System.out.println("tfMPS " + tfMPS + "\ntfMTP " + tfMTP
+" --> "+imageFileName);

 

                try {
                    xscale = tfMPS.getAsDouble(0);
                    yscale = tfMPS.getAsDouble(1);

 

                    double[] tiepoints = tfMTP.getAsDoubles();

 

                    System.out.println("count=" + tfMTP.getCount());

 

                    for (int i = 0; i < tiepoints.length; i++) {
                        System.out.println("-->" + tiepoints[i]);
                    }

 

                    System.out.println("type=" + tfMPS.getType());
                    System.out.println("type=" + tfMTP.getType());
                    System.out.println("nb of tiepoints = "
                            + tfMTP.getAsDouble(0));

 

                    if (tfMTP.getCount() != 6 && tfMTP.getCount() != 4)
                    {
                        System.out.println("warning tif file may pose
problems...");
                    }

 

                    if (tfMTP.getCount() == 6)
                    {
                        lonNW = tfMTP.getAsDouble(3);
                        latNW = tfMTP.getAsDouble(4);
                    }
                    else if (tfMTP.getCount() == 2)
                    {
                        lonNW = tfMTP.getAsDouble(0);
                        latNW = tfMTP.getAsDouble(1);
                    }
                    else
                    {
                        lonNW = tfMTP.getAsDouble(2);
                        latNW = tfMTP.getAsDouble(3);
                    }

 

                    DMSLatLonPoint upperLeft = new DMSLatLonPoint(new
LatLonPoint(latNW, lonNW));
                    DMSLatLonPoint lowerRight = new DMSLatLonPoint(new
LatLonPoint(latNW
                                    - (yscale * (source.getHeight())),
lonNW
                                    + (xscale * (source.getWidth()))));

 

                    System.out.println("xscale " + xscale + " yscale "
+yscale
                            + " lonNW " + lonNW + " latNW " + latNW
                            + " img height " + source.getHeight()
                            + " img width " + source.getWidth() + " "
                            + formatDMSPoint(upperLeft) + " "
                            + formatDMSPoint(lowerRight));
                    init((double) (latNW), (double) (lonNW),
                            (double) (latNW - (yscale *
(source.getHeight()))),
                            (double) (lonNW + (xscale *
(source.getWidth()))),
                            (double) xscale, (double) yscale);

 

                    st.close();
                } catch (Exception e) {
                    st.close();
                    System.out.println("Error reading geotiff" + e);
                    e.printStackTrace();
                    System.out
                            .println("No Geotiff referencing
found.Attempting projection file.");

 

                    if (imageFileName.endsWith(".tif"))
                        projfilename = imageFileName.replaceAll("\\.tif
<file:///\\.tif> ",
                                ".tfw");

 

                    setGeoReferencing(source, projfilename);
                }

 

            } else {

 

                if (imageFileName.endsWith(".bmp"))
                    projfilename = imageFileName.replaceAll("\\.bmp
<file:///\\.bmp> ",
".bpw");
                else if (imageFileName.endsWith(".BMP"))
                    projfilename = imageFileName.replaceAll("\\.BMP
<file:///\\.BMP> ",
".BPW");

 

                else if (imageFileName.endsWith(".JPG"))
                    projfilename = imageFileName.replaceAll("\\.JPG
<file:///\\.JPG> ",
".JGW");
                else if (imageFileName.endsWith(".jpg"))
                    projfilename = imageFileName.replaceAll("\\.jpg
<file:///\\.jpg> ",
".jgw");

 

                setGeoReferencing(source, projfilename);
            }
        } catch (IOException e) {
            System.out.println("OMJAIScalingRaster...");
            System.out.println(e.getMessage());
        }
    }

 

    public void setGeoReferencing(PlanarImage src, String projfilename)
throws FileNotFoundException
    {
        double xscale;
        double yscale;
        double lonNW;
        double latNW;
        
        try
        {
            FileReader projectionFile;
            projectionFile = new FileReader(projfilename);
            BufferedReader reader = new BufferedReader(projectionFile);
            String line = reader.readLine();
            System.out.println("xscale=" + line);
            xscale = Double.valueOf(line).doubleValue();

 

            line = reader.readLine();
            line = reader.readLine();
            line = reader.readLine();
            System.out.println("yscale=" + line);
            yscale = -Double.valueOf(line).doubleValue();
            // System.out.println(line);
            line = reader.readLine();
            lonNW = Double.valueOf(line).doubleValue();
            System.out.println("lonNW=" + line);
            line = reader.readLine();
            latNW = Double.valueOf(line).doubleValue();
            System.out.println("latNW=" + line + " width=" + sourceWidth
                    + " sourceHeigth" + sourceHeigth);

 

            // reader.close();
            projectionFile.close();
            reader.close();

 

            // if (xsize == -1 || ysize == -1)
            // {
            init((double) (latNW), (double) (lonNW),
                    (double) (latNW - (xscale * (sourceHeigth))),
                    (double) (lonNW + (yscale * (sourceWidth))),
                    (double) xscale, (double) yscale);
            // }
            // else
            // init((double) (latNW), (double) (lonNW), (double) (latNW
-
            // (xscale * ysize)), (double) (lonNW + (yscale *
xsize)),(double)
            // xscale, (double) yscale);

 

        }
        catch (IOException e)
        {
            System.out.println("setgeo :" + e.getMessage());
        }
    }

 

    void init(double lt, double ln, double lt_2, double ln_2, double
xim_scale,double yim_scale)
    {
        lat = (float) lt;
        lon = (float) ln;

 

        ximage_scale = xim_scale;
        yimage_scale = yim_scale;
        // sourcelat = lt;
        // sourcelon = lon;

 

        lat2 = lt_2;
        lon2 = ln_2;

 

        System.out.println("lat " + lat + " lon " + lon + " lat2 " +
lat2+ " lon2 " + lon2);

 

    }

 

    private String formatDMSPoint(DMSLatLonPoint p)
    {
        String res = "[[";
        res += p.lat_isnegative ? "-" : "";
        res += p.lat_degrees + " " + p.lat_minutes + " " + p.lat_seconds
+ "] [";
        res += p.lon_isnegative ? "-" : "";
        res += p.lon_degrees + " " + p.lon_minutes + " " +
p.lon_seconds+ "]]";
        return res;
    }
}





________________________________

Need a new ride? Check out the largest site for U.S. used car listings
at AOL Autos <http://autos.aol.com/used?NCID=aolcmp00300000002851> .



--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Wed Apr 23 2008 - 07:47:55 EDT

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:09 EDT