raster.java

来自「geotools的源码」· Java 代码 · 共 1,460 行 · 第 1/3 页

JAVA
1,460
字号
* @throws java.io.IOException
* @deprecated new programs should use writeObject.
*/
public void save(String name) throws java.io.IOException{
    DataOutputStream out = new DataOutputStream(new FileOutputStream(name));
    out.writeUTF(this.name);
    out.writeDouble(originx);
    out.writeDouble(originy);
    out.writeInt(height);
    out.writeInt(width);
    out.writeDouble(cellsize);
    out.writeDouble(nodata);
    //out.writeDouble(TOL);
    out.writeBoolean(sparse);
    out.writeDouble(sparseness);
    if(sparse){
        RCVData a[]=getRCVData();
        for(int i=0;i<a.length;i++){
            out.writeInt(a[i].row);
            out.writeInt(a[i].col);
            out.writeDouble(a[i].value);
        }
    }else{
        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){
                out.writeDouble(getCell(i,j));
            }
        }
    }
}

/** 
* load a raster from a URL 
* @throws java.io.IOException
*/
public static Raster load(URL url) throws java.io.IOException{
    URLConnection uc = url.openConnection();
    int len = uc.getContentLength();
    if(len <=0){
        throw new IOException("File fetched from URL was of zero length or could not be found");
    }
    byte data[];
    data = new byte[len];
    BufferedInputStream inn = new BufferedInputStream(uc.getInputStream());
    int jj=0,k=0;
    while(k<len || jj==-1){
        jj = inn.read(data,k,len-k);
        k+=jj;
    }
    ByteArrayInputStream bais = new ByteArrayInputStream (data);
    BufferedInputStream bin = new BufferedInputStream(bais);
    DataInputStream in = new DataInputStream(bin);
    return streamLoad (in);
}

/** 
* load a raster from file named in name.
* @throws java.io.IOException
* @deprecated new programs should use readObject.
*/
public static Raster load(String name) throws java.io.IOException{
    InputStream dis = new FileInputStream(name);
    BufferedInputStream bin = new BufferedInputStream(dis);
    DataInputStream in = new DataInputStream(bin);
    return streamLoad (in);
}
	
/** 
* load a raster from the Stream.
* @throws java.io.IOException
* @deprecated new programs should use readObject.
*/
public static Raster streamLoad (DataInputStream in) throws java.io.IOException {
    String fname=in.readUTF();
    double originx=in.readDouble();
    double originy=in.readDouble();
    int height=in.readInt();
    int width=in.readInt();
    double cellsize=in.readDouble();
    GeoRectangle m = new GeoRectangle(originx,originy,(double)width*(double)cellsize,(double)height*(double)cellsize);
    double nodata=in.readDouble();
    //double TOL=in.readDouble();
    boolean sparse=in.readBoolean();
    double sparseness=in.readDouble();
    if(sparse){
        //Raster out=new Raster();
        Raster.RCVData a[]=new RCVData[(int)sparseness];
        for(int i=0;i<a.length;i++){
            a[i]=new RCVData();
            a[i].row=in.readInt();
            a[i].col=in.readInt();
            a[i].value=in.readDouble();
        }
        Raster out=new Raster(m,cellsize,a);
        //out.setName(name);
        return out;
    }else{
        double c[]=new double[height*width];
        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){
                c[i*width+j]=in.readDouble();
            }
        }
        Raster out=new Raster(m,cellsize,c);
        //out.setName(name);
        return out;
    }
}
		
/**
 * convert the data of the raster into an array of XYVData objects. 
 *
 * XYVData is a portable way of passing raster data about.
 * @return an array of XYVData objects.
 */
public XYVData[] getXYVData(){
    XYVData out[]=new XYVData[(int)sparseness];
    double val;
    int p=-1;
    if(sparseness==0.0){
        return null;
    }
    for(int i=0;i<height;i++){
        for(int j=0;j<width;j++){
            val=getCell(i,j);
            //if(DEBUG)System.out.println("Ra-->"+i+" "+j+" "+val);
            if(Math.abs(val-0.0d)>=TOL){
                p++;
                out[p]=new XYVData();
                out[p].value=val;
                out[p].x=getXCell(j);
                out[p].y=getYCell(i);
                //if(DEBUG)System.out.println("Ra-->j "+j+" -> x "+out[p].x);
                //if(DEBUG)System.out.println("Ra-->i "+i+" -> y "+out[p].y);
            }
        }
    }
    p++;
    if(p!=0)return(out);
    else
    return null;
}

public RCVData[] getRCVData(){
    RCVData out[]=new RCVData[(int)sparseness];
    double val;
    int p=0;
    for(int i=0;i<height;i++){
        for(int j=0;j<width;j++){
            val=getCell(i,j);
            if(Math.abs(val-0.0d)>TOL){
                out[p]=new RCVData();
                out[p].value=val;
                out[p].row=i;
                out[p].col=j;
                p++;
            }
        }
    }
    return(out);
}
		
public Object clone(){
    //if(DEBUG)System.out.println("Ra-->cloning ");
    Raster t= new Raster(getBounds(),getCellSize(),(XYVData[])null);
    t.sparse = this.sparse;
    t.sparseness = this.sparseness;
    t.TOL = this.TOL;
    if(t.sparse){
        t.scells=(Hashtable)this.scells.clone();
    }else{
        t.cells=new double[width*height];
        System.arraycopy(this.cells,0,t.cells,0,this.cells.length);
    }
    t.min=this.min;
    t.nzmin=this.nzmin;
    t.max=this.max;
    return t;
}

/**
 * @param a the raster to be added
 * @exception RasterMathException if the raster cellsizes differ
 */
public Raster RasterMath(Raster a,RasterOp op) throws RasterMathException{
    if(a==null) {
        throw new RasterMathException("Null Raster");
    }
    if(getCellSize()!=a.getCellSize()){
        // this won't work if the rasters are different scales
        throw new RasterMathException("Cell Size Mismatch\n "+getName()+" "+getCellSize()+" "+a.getName()+" "+a.getCellSize());
    }
    GeoRectangle out = new GeoRectangle(getBounds());
    out.add(a.getBounds());
    //Raster t1=(Raster)this.clone();
    Raster t1=this;
    GeoRectangle inter = getBounds().createIntersect(a.getBounds());
    t1.setBounds(out);
    //Raster t2 = new Raster(out,cellsize,d2);
    //Raster t2=(Raster)a.clone();
    Raster t2=a;
    //t2.setBounds(out);
    //t1.setName(getName());
    //t2.setName(a.getName());
    Raster t3 = new Raster(out,cellsize,(RCVData [])null);
    if(t1.getSparseness()==100.0d){
        return (Raster)t2.clone();
    }
    if(t2.getSparseness()==100.0d){
        return (Raster)t1.clone();
    }
    // now step through the two rasters applying op to them.
    if(!(t1.sparse&&t2.sparse)){ // if they are both sparse we can save time by just working through the non-zero cells
        double v;
        t3.setName("Output");
        t3.min=Double.MAX_VALUE;
        t3.nzmin=Double.MAX_VALUE;
        t3.max=Double.MIN_VALUE;
        for(int i=0;i<inter.height;i++){
            for(int j=0;j<inter.width;j++){
                v=op.calc(t1.getCell(i,j),t2.getCell(i,j));
                if(!Double.isInfinite(v)&&!Double.isNaN(v)){
                    t3.min=Math.min(t3.min,v);
                    if(v>0.0) t3.nzmin=Math.min(t3.nzmin,v);
                    t3.max=Math.max(t3.max,v);
                    t3.putCell(i,j,v);
                }
            }
        }
        return t3;
    }else{
        Double v1,v2;
        double v;
        t3.min=Double.MAX_VALUE;
        t3.nzmin=Double.MAX_VALUE;
        t3.max=Double.MIN_VALUE;
        MyInt pos;
        // this probably doesn't work properly if a op b != b op a and 0 is not an identity operation.
        java.util.Enumeration k = t1.scells.keys();
        while (k.hasMoreElements()){
            pos = (MyInt) k.nextElement();
            v1=(Double)t1.scells.get(pos);
            if(t2.scells.containsKey(pos)){
                v2=(Double)t2.scells.get(pos);
                v=op.calc(v1.doubleValue(),v2.doubleValue());
            }else{
                v=op.calc(v1.doubleValue(),0.0d);
            }
            if(Math.abs(v-0.0d)>TOL){
                t3.sparseness++;
            }
            if(!Double.isInfinite(v)&&!Double.isNaN(v)){
                t3.scells.put(pos, new Double(v));
                t3.min=Math.min(t3.min,v);
                if(v>0.0) t3.nzmin=Math.min(t3.nzmin,v);
                if(v!=Double.POSITIVE_INFINITY)t3.max=Math.max(t3.max,v);
            }
        }
        if(t3.min>0.0d) t3.min=0.0d; // no zeros in a sparse raster
        return t3;
    }
}

public final Raster RasterMath(double a,RasterOp op) throws RasterMathException{
    double v;
    Raster t1=(Raster)this.clone();
    if(a==0.0d&&op instanceof mult){ // mult by zero is zero
        t1.scells=new Hashtable();
        t1.cells=new double[1];
        t1.min=0.0d;
        t1.max=0.0d;
        t1.sparse=true;
        t1.sparseness=0.0;
        return t1;
    }
    if(a==1.0d&&(op instanceof mult||op instanceof divide)){ // identity
        return t1;
    }
    if(a==0.0d&&(op instanceof add||op instanceof sub)){ // identity
        return t1;
    }
    t1.min=Double.MAX_VALUE;
    t1.max=Double.MIN_VALUE;
    if(!t1.sparse){
        for(int i=0;i<t1.height;i++){
            for(int j=0;j<t1.width;j++){
                v=op.calc(getCell(i,j),a);
                t1.min=Math.min(t1.min,v);
                if(v>0.0)t1.nzmin=Math.min(t1.nzmin,v);
                t1.max=Math.max(t1.max,v);
                t1.putCell(i,j,v);
            }
        }
    }else{ /* sparse*/
        Double v1;
        MyInt pos;
        java.util.Enumeration k = t1.scells.keys();
        while (k.hasMoreElements()){
            pos = (MyInt) k.nextElement();
            v1=(Double)t1.scells.get(pos);
            t1.scells.remove(pos);
            v=op.calc(v1.doubleValue(),a);
            if(Math.abs(v-0.0d)>TOL&&Math.abs(v1.doubleValue()-0.0d)<TOL)
            t1.sparseness++; 
            t1.scells.put(pos,new Double(v));
            t1.min=Math.min(t1.min,v);
            t1.max=Math.max(t1.max,v);
        }
        t1.nzmin=t1.min;
        if(t1.min>0.0d)t1.min=0.0d; // if its sparse then we haven't seen any zeros
    }
    return t1;
}

public java.util.Enumeration getIds(){
    return new ID();
}

    class ID implements java.util.Enumeration{

    boolean more = true;
    int next=0;

        public boolean hasMoreElements(){
            return more;
        }

        public Object nextElement(){
            Integer n = new Integer(next++);
            if(next>width*height){
                more = false;
            }
            return n;
        }
    }


private void writeObject(java.io.ObjectOutputStream out) throws IOException{
    out.writeUTF(this.name);
    out.writeDouble(originx);
    out.writeDouble(originy);
    out.writeInt(height);
    out.writeInt(width);
    out.writeDouble(cellsize);
    out.writeDouble(nodata);
    //out.writeDouble(TOL);
    out.writeBoolean(sparse);
    out.writeDouble(sparseness);
    if(sparse){
        RCVData a[]=getRCVData();
        for(int i=0;i<a.length;i++){
            out.writeInt(a[i].row);
            out.writeInt(a[i].col);
            out.writeDouble(a[i].value);
        }
    }else{
        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){
                out.writeDouble(getCell(i,j));
            }
        }
    }
}

private Object readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{ 
    String fname=in.readUTF();
    double originx=in.readDouble();
    double originy=in.readDouble();
    int height=in.readInt();
    int width=in.readInt();
    double cellsize=in.readDouble();
    GeoRectangle m = new GeoRectangle(originx,originy,(double)width*(double)cellsize,(double)height*(double)cellsize);
    double nodata=in.readDouble();
    //double TOL=in.readDouble();
    boolean sparse=in.readBoolean();
    double sparseness=in.readDouble();
    if(sparse){
        //Raster out=new Raster();
        Raster.RCVData a[]=new RCVData[(int)sparseness];
        for(int i=0;i<a.length;i++){
            a[i]=new RCVData();
            a[i].row=in.readInt();
            a[i].col=in.readInt();
            a[i].value=in.readDouble();
        }
        Raster out=new Raster(m,cellsize,a);
        //out.setName(name);
        return out;
    }else{
        double c[]=new double[height*width];
        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){
                c[i*width+j]=in.readDouble();
            }
        }
        Raster out=new Raster(m,cellsize,c);
        out.setName(name);
        return out;
    }
}

static public void main(String args[]){
    // test function
    Raster t1=null,t2=null;
    ObjectInputStream in ;
    long start,inter;
    start=System.currentTimeMillis();
    try{
        in = new ObjectInputStream(new BufferedInputStream(new
        FileInputStream("/home/preston/ian/java/javashape/w3acc4.ras"),2048));
        t1=(Raster)in.readObject();
        inter=System.currentTimeMillis();
        if(DEBUG)System.out.println("Ra-->Loaded 1st file "+((inter-start)/1000));
        start=System.currentTimeMillis();
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("/home/preston/ian/java/javashape/w3pop4.ras"),10000));
        t2=(Raster)in.readObject();
        inter=System.currentTimeMillis();
        if(DEBUG)System.out.println("Ra-->Loaded 2nd file "+((inter-start)/1000));
    }
    catch(IOException e){System.out.println("Ra-->ArcInfo Load Failed "+e);}
    catch(ClassNotFoundException cne){System.out.println("Ra-->Class not found "+cne);}
    try{
        start=System.currentTimeMillis();
        t1=t1.RasterMath(t2,new add());
        inter=System.currentTimeMillis();
        if(DEBUG)System.out.println("Ra-->added "+((inter-start)/1000));
    }catch(RasterMathException e){System.out.println("Ra-->"+e);}
}

/* this sort of fudges the changes up to the layer */
Vector listeners = new Vector();

public void removeChangedListener(ChangedListener lcl) {
    listeners.removeElement(lcl);
}

public void addChangedListener(ChangedListener lcl){
   listeners.addElement(lcl);
}

public void notifyChangedListeners(int reason){
    Vector l;
    ChangedEvent lce = new ChangedEvent(this,reason);
    synchronized(this) {l = (Vector)listeners.clone(); }
    for (int i = 0; i < l.size();i++) {
        ((ChangedListener)l.elementAt(i)).Changed(lce);
    }
}

public void writeArcGrid(File f){
    BufferedOutputStream os ;
    try{
        os = new BufferedOutputStream(new FileOutputStream(f));
    }catch(Exception e){System.err.println("Ra-->Error in save "+e);return;}
    PrintWriter pw = new PrintWriter(os);
    pw.println("Ncols "+width+"\nnrows "+height);
    pw.println("xllcorner "+originx+"\nyllcorner "+originy);
    pw.println("cellsize "+cellsize);
    pw.println("NODATA_value "+nodata);
    for(int j=0;j<height;j++){
        for(int i=0;i<width;i++){
            pw.print(""+getCell(j,i)+" ");
        }
        pw.println();
    }
    pw.close();
}

    class MyInt {
    int value;
    MyInt(){}
    MyInt(int v){
        value=v;
    }
        void setValue(int v){
            value=v;
        }
        int intValue(){
            return value;
        }
        public int hashCode(){return value;}
        public String toString(){return ""+value;}
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?