Revision 22310

View differences:

org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
    <modelVersion>4.0.0</modelVersion>
4
    <artifactId>org.gvsig.raster.gdal.io</artifactId>
5
    <packaging>jar</packaging>
6
    <name>${project.artifactId}</name>
7
    <parent>
8
        <groupId>org.gvsig</groupId>
9
        <artifactId>org.gvsig.raster.gdal</artifactId>
10
        <version>2.2.252</version>
11
    </parent>
12
	
13
    <dependencies>
14
        <dependency>
15
            <groupId>org.gdal</groupId>
16
            <artifactId>gdal</artifactId>
17
            <scope>compile</scope>
18
        </dependency>
19
        <dependency>
20
            <groupId>org.gvsig</groupId>
21
            <artifactId>org.gvsig.raster.cache.lib.api</artifactId>
22
            <scope>compile</scope>
23
        </dependency>
24
        <dependency>
25
            <groupId>org.gvsig</groupId>
26
            <artifactId>org.gvsig.raster.lib.api</artifactId>
27
            <scope>compile</scope>
28
        </dependency>
29
        <dependency>
30
            <groupId>org.gvsig</groupId>
31
            <artifactId>org.gvsig.raster.lib.impl</artifactId>
32
            <scope>compile</scope>
33
        </dependency>
34
        <dependency>
35
            <groupId>org.gvsig</groupId>
36
            <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
37
            <scope>compile</scope>
38
        </dependency>
39
        <dependency>
40
            <groupId>org.gvsig</groupId>
41
            <artifactId>org.gvsig.remoteclient</artifactId>
42
            <scope>compile</scope>
43
        </dependency>
44
        <dependency>
45
            <groupId>org.gvsig</groupId>
46
            <artifactId>org.gvsig.tools.lib</artifactId>
47
            <scope>compile</scope>
48
        </dependency>
49
        <dependency>
50
            <groupId>org.gvsig</groupId>
51
            <artifactId>org.gvsig.projection.api</artifactId>
52
            <scope>compile</scope>
53
        </dependency>
54
        <dependency>
55
            <groupId>org.gvsig</groupId>
56
            <artifactId>org.gvsig.projection.cresques.impl</artifactId>
57
            <scope>runtime</scope>
58
        </dependency>
59
        <dependency>
60
            <groupId>org.gvsig</groupId>
61
            <artifactId>org.gvsig.compat.api</artifactId>
62
            <scope>compile</scope>
63
        </dependency>
64
        <dependency>
65
            <groupId>org.gvsig</groupId>
66
            <artifactId>org.gvsig.compat.se</artifactId>
67
            <scope>runtime</scope>
68
        </dependency>
69
        <dependency>
70
            <groupId>org.gvsig</groupId>
71
            <artifactId>org.gvsig.fmap.dal.api</artifactId>
72
            <scope>compile</scope>
73
        </dependency>
74
        <dependency>
75
            <groupId>org.gvsig</groupId>
76
            <artifactId>org.gvsig.fmap.dal.impl</artifactId>
77
            <scope>runtime</scope>
78
        </dependency>
79
        <dependency>
80
            <groupId>org.gvsig</groupId>
81
            <artifactId>org.gvsig.fmap.dal.file.lib</artifactId>
82
            <scope>compile</scope>
83
        </dependency>
84
        <dependency>
85
            <groupId>org.gvsig</groupId>
86
            <artifactId>org.gvsig.fmap.dal.spi</artifactId>
87
            <scope>compile</scope>
88
        </dependency>
89
        <dependency>
90
            <groupId>org.gvsig</groupId>
91
            <artifactId>org.gvsig.fmap.geometry.api</artifactId>
92
            <scope>compile</scope>
93
        </dependency>
94

  
95
    </dependencies>
96

  
97

  
98
</project>
0 99

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.raster.gdal.io.DefaultGdalIOLibrary
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/addo/Jaddo.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22
package org.gvsig.addo;
23
import java.io.File;
24

  
25
import org.gdal.gdal.Dataset;
26
import org.gdal.gdal.gdal;
27

  
28
/**
29
 * Clase para la construccion de overviews de un raster.
30
 *
31
 * 15-nov-2007
32
 * @author Nacho Brodin (nachobrodin@gmail.com)
33
 */
34
public class Jaddo implements IOverviewIncrement{
35
	public final static int NEAREST = 0;
36
	public final static int AVERAGE = 1;
37
	public final static int AVERAGE_MP = 2;
38
	public final static int AVERAGE_MAGPHASE = 3;
39
	public final static int MODE = 4;
40

  
41
	private IOverviewIncrement  incrementListener = null;
42
	private int value = 0;
43

  
44
//	private native int buildOverviewsNative(int resamplingAlg, String file, int[] overviews);
45

  
46
	/**
47
	 * Construccion de overviews
48
	 */
49
	public void buildOverviews(int resamplingAlg, String file, int[] overviews)
50
		throws BuildingOverviewsException, WritingException {
51
		File f = new File(file);
52
		if(!f.exists() || !f.isFile())
53
			throw new BuildingOverviewsException("File does not exist.");
54
		if(!f.canWrite())
55
			throw new WritingException("File is not writeable");
56
		Dataset data = gdal.Open(file);
57
		if(data.BuildOverviews(overviews) >= 0)
58
			throw new BuildingOverviewsException("Problems building overviews");
59
	}
60

  
61
	/**
62
	 * Devuelve el porcentaje del incremento.
63
	 * @return int
64
	 */
65
	public int getPercent() {
66
		return value;
67
	}
68

  
69
	/**
70
	 * Asigna el porcentaje de incremento de la construccion de overview.
71
	 * Esto se hace automaticamente desde el callback que asigna el porcentaje.
72
	 */
73
	public void setPercent(int value) {
74
		this.value = value;
75
		if(incrementListener != null)
76
			incrementListener.setPercent(value);
77
	}
78

  
79
	/**
80
	 * Asigna el listener para la asignacion del incremento
81
	 * @param incr IOverviewIncrement
82
	 */
83
	public void setIncrementListener(IOverviewIncrement incr) {
84
		this.incrementListener = incr;
85
	}
86
}
0 87

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/addo/WritingException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.addo;
23

  
24
/**
25
 * Es generada no se puede escribir las overviews
26
 * 
27
 * @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
28
 * @version 0.0
29
 * @link http://www.gvsig.org
30
 */
31

  
32

  
33
public class WritingException extends Exception {
34

  
35
	public WritingException(String msg){
36
		super(msg);
37
	}
38
	
39
	
40
}
0 41

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/addo/IOverviewIncrement.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.addo;
23

  
24
/**
25
 * Interfaz para la asignacion y recuperacion del incremento
26
 * de la tarea.
27
 *
28
 * 17-nov-2007
29
 * @author Nacho Brodin (nachobrodin@gmail.com)
30
 */
31
public interface IOverviewIncrement {
32
	/**
33
	 * Devuelve el porcentaje del incremento
34
	 * @return int
35
	 */
36
	public int getPercent();
37
	
38
	/**
39
	 * Asigna el porcentaje de incremento de la construccion de overview.
40
	 * Esto se hace automaticamente desde el callback que asigna el porcentaje. 
41
	 */
42
	public void setPercent(int value);
43
}
0 44

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/addo/BuildingOverviewsException.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.addo;
23

  
24
/**
25
 * Es generada cuando hay algun problema en la construccion de 
26
 * las overviews y la funcion nativa devuelve 0
27
 * 
28
 * @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
29
 * @version 0.0
30
 * @link http://www.gvsig.org
31
 */
32
public class BuildingOverviewsException extends Exception {
33

  
34
	public BuildingOverviewsException(String msg) {
35
		super(msg);
36
	}
37
	
38
}
0 39

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/raster/gdal/overview/GdalOverviewBuilder.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.gdal.overview;
23

  
24
import org.gvsig.addo.BuildingOverviewsException;
25
import org.gvsig.addo.IOverviewIncrement;
26
import org.gvsig.addo.Jaddo;
27
import org.gvsig.addo.WritingException;
28
import org.gvsig.fmap.dal.coverage.exception.OverviewException;
29
import org.gvsig.fmap.dal.coverage.process.BaseIncrementableTask;
30
import org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder;
31

  
32
/**
33
 * Servicios ofrecidos por el proceso de construcci?n de overviews. Esta
34
 * implementaci?n usa gdal para construir las overviews.
35
 * 
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class GdalOverviewBuilder implements OverviewBuilder {
39
	private Jaddo        build         = new Jaddo();
40
	private int          value         = 0;
41

  
42
	/**
43
	 * Bindings between an IOverviewIncrement and a BaseIncrementableTask
44
	 * @author Nacho Brodin (nachobrodin@gmail.com)
45
	 */
46
	class ProcessBinding implements IOverviewIncrement {
47
		private BaseIncrementableTask  task = null;
48
		
49
		public ProcessBinding(BaseIncrementableTask task) {
50
			this.task = task;	
51
		}
52
		
53
		public int getPercent() {
54
			return task.getPercent();
55
		}
56

  
57
		public void setPercent(int value) {
58
			task.setPercent(value);
59
		}
60
	}
61
	
62
	/*
63
	 * (non-Javadoc)
64
	 * @see org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder#buildOverviews(int, java.lang.String, int[])
65
	 */
66
	public void buildOverviews(int resamplingAlg, String file, int[] overviews)
67
			throws OverviewException {
68
		try {
69
			build.buildOverviews(resamplingAlg, file, overviews);
70
		} catch (BuildingOverviewsException e) {
71
			throw new OverviewException("Error building overviews", e);
72
		} catch (WritingException e) {
73
			throw new OverviewException("Error writing overviews", e);
74
		}
75
	}
76

  
77
	/*
78
	 * (non-Javadoc)
79
	 * @see org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder#getPercent()
80
	 */
81
	public int getPercent() {
82
		return value;
83
	}
84

  
85
	/*
86
	 * (non-Javadoc)
87
	 * @see org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder#setIncrementListener(org.gvsig.addo.IOverviewIncrement)
88
	 */
89
	public void setIncrementListener(BaseIncrementableTask incr) {
90
		build.setIncrementListener(new ProcessBinding(incr));
91
	}
92

  
93
	/*
94
	 * (non-Javadoc)
95
	 * @see org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder#setPercent(int)
96
	 */
97
	public void setPercent(int value) {
98
		this.value = value;
99
	}
100
}
0 101

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/raster/gdal/io/GdalNewRasterStoreParameters.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22
package org.gvsig.raster.gdal.io;
23

  
24
import org.gvsig.fmap.dal.DataStoreParameters;
25
import org.gvsig.raster.impl.store.AbstractNewRasterStoreParameters;
26

  
27
/**
28
 * Parameters for creating a Gdal store
29
 * @author Nacho Brodin (nachobrodin@gmail.com)
30
 */
31
public class GdalNewRasterStoreParameters extends AbstractNewRasterStoreParameters {
32

  
33
	public String getDataStoreName() {
34
		return GdalProvider.NAME;
35
	}
36

  
37
	public String getDescription() {
38
		return GdalProvider.DESCRIPTION;
39
	}
40

  
41
    @Override
42
    public boolean isTheSameStore(DataStoreParameters dsp) {
43
        return this.equals(dsp);
44
    }
45
}
0 46

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/raster/gdal/io/GdalFilesystemServerExplorer.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
package org.gvsig.raster.gdal.io;
29

  
30
import java.awt.geom.AffineTransform;
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.IOException;
34

  
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataServerExplorer;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.NewDataStoreParameters;
40
import org.gvsig.fmap.dal.coverage.RasterLocator;
41
import org.gvsig.fmap.dal.coverage.datastruct.Params;
42
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
43
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
44
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
45
import org.gvsig.fmap.dal.coverage.store.DataServerWriter;
46
import org.gvsig.fmap.dal.coverage.store.RasterWriter;
47
import org.gvsig.fmap.dal.coverage.store.parameter.NewRasterStoreParameters;
48
import org.gvsig.fmap.dal.exception.CreateException;
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.exception.RemoveException;
51
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider;
52
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
53
import org.gvsig.raster.impl.store.AbstractRasterFileDataParameters;
54
import org.gvsig.tools.locator.LocatorException;
55

  
56
public class GdalFilesystemServerExplorer extends AbstractFilesystemServerExplorerProvider {
57
	
58
	public boolean canCreate() {
59
		return false;
60
	}
61

  
62
	public boolean canCreate(NewDataStoreParameters parameters) {
63
		return false;
64
	}
65

  
66
	public void create(NewDataStoreParameters parameters, boolean overwrite)
67
			throws CreateException {
68
		NewRasterStoreParameters p = null;
69
		if(parameters instanceof NewRasterStoreParameters)
70
			 p = (NewRasterStoreParameters)parameters;
71
		
72
		DataServerWriter dataWriter = p.getDataServer();
73
		if(dataWriter == null)
74
			dataWriter = RasterLocator.getManager().createDataServerWriter();
75
		
76
		dataWriter.setBuffer(p.getBuffer(), p.getBand());
77
		Params params;
78
		try {
79
			if(p.getDriverParams() != null)
80
				params = p.getDriverParams();
81
			else
82
				params = RasterLocator.getManager().createWriter(p.getFileName()).getParams();
83
			
84
			AffineTransform affineTransform = p.getAffineTransform();
85
			if(affineTransform == null) {
86
				if(p.getBuffer().getDataExtent() != null) {
87
					double x = p.getBuffer().getDataExtent().getMinX();
88
					double y = p.getBuffer().getDataExtent().getMaxY();
89
					double pixelSizeX = p.getBuffer().getDataExtent().getWidth() / p.getBuffer().getWidth();
90
					double pixelSizeY = p.getBuffer().getDataExtent().getHeight() / p.getBuffer().getHeight();
91
					affineTransform = new AffineTransform(pixelSizeX, 0, 0, -pixelSizeY, x, y);
92
				} else {
93
					affineTransform = new AffineTransform();
94
				}
95
			}
96
			
97
			RasterWriter writer = RasterLocator.getManager().createWriter(
98
					dataWriter, 
99
					p.getPath() + File.separator + p.getFileName(),
100
					p.getBand() < 0 ? p.getBuffer().getBandCount() : 1, 
101
					affineTransform, 
102
					p.getBuffer().getWidth(),
103
					p.getBuffer().getHeight(), 
104
					p.getBuffer().getDataType(), 
105
					params, 
106
					null);
107
			if(p.getColorInterpretation() != null)
108
				writer.setColorBandsInterpretation(p.getColorInterpretation());
109
			writer.setWkt(p.getWktProjection());
110
			
111
			writer.dataWrite();
112
			writer.writeClose();
113
		} catch (LocatorException e) {
114
			throw new CreateException("", e);
115
		} catch (NotSupportedExtensionException e) {
116
			throw new CreateException("", e);
117
		} catch (RasterDriverException e) {
118
			throw new CreateException("", e);
119
		} catch (ProcessInterruptedException e) {
120
			//Fin del proceso
121
		} catch (IOException e) {
122
			throw new CreateException("", e);
123
		}
124
	}
125

  
126
	public NewDataStoreParameters getCreateParameters() throws DataException {
127
		return new GdalNewRasterStoreParameters();
128
	}
129

  
130
	public void initialize(FilesystemServerExplorerProviderServices serverExplorer) {
131
	}
132
	
133
	public void remove(DataStoreParameters parameters) throws RemoveException {
134
		throw new UnsupportedOperationException();
135
	}
136

  
137
	public String getDataStoreProviderName() {
138
		return GdalProvider.NAME;
139
	}
140

  
141
	public boolean accept(File pathname) {
142
		if (pathname.getParentFile() != null && 
143
			pathname.getParentFile().getName().equals("cellhd")) {
144
			if (pathname.getName().endsWith(".rmf")
145
					|| pathname.getName().endsWith(".rmf~")) {
146
				return false;
147
			}
148
			return true;
149
		}
150

  
151
		// Comprobamos que no sea un rmf propio, osea, que contenga xml
152
		if (pathname.getName().toLowerCase().endsWith(".rmf")) {
153
			FileInputStream reader = null;
154
			try {
155
				reader = new FileInputStream(pathname);
156
				String xml = "";
157
				for (int i = 0; i < 6; i++) {
158
					xml += (char) reader.read();
159
				}
160
				if (xml.equals("<?xml ")) {
161
					return false;
162
				}
163
			} catch (Exception e) {
164
			} finally {
165
				try {
166
					reader.close();
167
				} catch (Exception e) {
168
				}
169
			}
170
		}
171
		return RasterLocator.getManager().getProviderServices().isExtensionSupported(
172
				pathname.getAbsolutePath(), 
173
				GdalProvider.class);
174
	}
175

  
176
	public String getDescription() {
177
		return GdalProvider.DESCRIPTION;
178
	}
179

  
180
	public DataStoreParameters getParameters(File file) throws DataException {
181
		DataManager manager = DALLocator.getDataManager();
182
		AbstractRasterFileDataParameters params = (AbstractRasterFileDataParameters) manager
183
				.createStoreParameters(this.getDataStoreProviderName());
184
		params.setFile(file);
185
		return params;
186
	}
187
	
188
	public int getMode() {
189
		return DataServerExplorer.MODE_RASTER;
190
	}
191

  
192
}
0 193

  
org.gvsig.raster.gdal/tags/org.gvsig.raster.gdal-2.2.252/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/raster/gdal/io/GdalWriter.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.gdal.io;
23

  
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.Point2D;
26
import java.io.File;
27
import java.io.IOException;
28
import java.sql.Savepoint;
29
import java.util.ArrayList;
30

  
31
import org.apache.commons.io.FileUtils;
32
import org.apache.commons.io.FilenameUtils;
33
import org.cresques.cts.ICRSFactory;
34
import org.cresques.cts.IProjection;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

  
38
import org.gvsig.fmap.dal.coverage.RasterLibrary;
39
import org.gvsig.fmap.dal.coverage.RasterLocator;
40
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
41
import org.gvsig.fmap.dal.coverage.datastruct.Params;
42
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
43
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
44
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
45
import org.gvsig.fmap.dal.coverage.store.DataServerWriter;
46
import org.gvsig.jgdal.GdalBuffer;
47
import org.gvsig.jgdal.GdalDataset;
48
import org.gvsig.jgdal.GdalDriver;
49
import org.gvsig.jgdal.GdalException;
50
import org.gvsig.jgdal.GdalRasterBand;
51
import org.gvsig.jgdal.GeoTransform;
52
import org.gvsig.raster.gdal.io.features.BMPFeatures;
53
import org.gvsig.raster.gdal.io.features.GTiffFeatures;
54
import org.gvsig.raster.gdal.io.features.HFAFeatures;
55
import org.gvsig.raster.gdal.io.features.IDRISIFeatures;
56
import org.gvsig.raster.gdal.io.features.ILWIS_MprFeatures;
57
import org.gvsig.raster.gdal.io.features.Jpeg2000Features;
58
import org.gvsig.raster.gdal.io.features.PNM_PgmFeatures;
59
import org.gvsig.raster.gdal.io.features.PNM_PpmFeatures;
60
import org.gvsig.raster.impl.buffer.DefaultDataServerWriter;
61
import org.gvsig.raster.impl.buffer.RasterBuffer;
62
import org.gvsig.raster.impl.process.RasterTask;
63
import org.gvsig.raster.impl.process.RasterTaskQueue;
64
import org.gvsig.raster.impl.store.ParamImpl;
65
import org.gvsig.raster.impl.store.ParamsImpl;
66
import org.gvsig.raster.impl.store.WriteFileFormatFeatures;
67
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
68
import org.gvsig.raster.impl.store.writer.DefaultRasterWriter;
69
import org.gvsig.raster.util.DefaultProviderServices;
70
import org.gvsig.tools.ToolsLocator;
71
import org.gvsig.tools.extensionpoint.ExtensionPoint;
72
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
73

  
74

  
75
/**
76
 * Driver para la escritura a trav?s de Gdal.
77
 * Puede exportar un fichero de un formato a otro desde un GeoRasterFile
78
 * en cualquier formato soportado por la lectura a un formato que este incluido
79
 * en la lista supportedDrv.
80
 *
81
 * Puede salvar a disco en un formato que este incluido en la lista supportedDrv
82
 * obteniendo los datos que van siendo servidos desde el cliente. Este cliente
83
 * debe implementar un DataServerWriter o tener un objeto que lo implemente. Inicialmente
84
 * le pasar? los par?metros de la imagen de salida y cuando el driver comience a
85
 * escribir le ir? solicitando m?s a trav?s del m?todo readData de DataServerWriter.
86
 * El cliente ser? el que lleve el control de lo que va sirviendo y lo que le queda
87
 * por servir.
88
 * @author Nacho Brodin (nachobrodin@gmail.com)
89
 */
90
public class GdalWriter extends DefaultRasterWriter {
91

  
92
    private static final Logger logger = LoggerFactory.getLogger(GdalWriter.class);
93

  
94
	/**
95
	 *
96
	 */
97
	public static void register() {
98
		DefaultProviderServices pInfo = (DefaultProviderServices)RasterLocator.getManager().getProviderServices();
99
		ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
100
		ExtensionPoint point = extensionPoints.get("RasterWriter");
101

  
102
		point.append("tif", "", GdalWriter.class);
103
		pInfo.getFileFeature().put("tif", new GTiffFeatures());
104

  
105
		point.append("img", "", GdalWriter.class);
106
		pInfo.getFileFeature().put("img", new HFAFeatures());
107

  
108
		point.append("bmp", "", GdalWriter.class);
109
		pInfo.getFileFeature().put("bmp", new BMPFeatures());
110

  
111
		point.append("pgm", "", GdalWriter.class);
112
		pInfo.getFileFeature().put("pgm", new PNM_PgmFeatures());
113

  
114
		point.append("ppm", "", GdalWriter.class);
115
		pInfo.getFileFeature().put("ppm", new PNM_PpmFeatures());
116

  
117
		point.append("mpl", "", GdalWriter.class);
118
		pInfo.getFileFeature().put("mpl", new ILWIS_MprFeatures());
119

  
120
		point.append("rst", "", GdalWriter.class);
121
		pInfo.getFileFeature().put("rst", new IDRISIFeatures());
122

  
123
		point.append("jp2", "", GdalWriter.class);
124
		pInfo.getFileFeature().put("jp2", new Jpeg2000Features());
125

  
126
		//La exportaci?n no es correcta del todo
127
//		point.append("rmf", "", GdalWriter.class);
128
//		pInfo.getFileFeature().put("rmf", new RMFFeatures());
129

  
130
		//No salva datos. Siempre sale negra la imagen
131
		//point.register("aux", GdalWriter.class);
132
		//fileFeature.put("aux", new PAuxFeatures());
133
	}
134

  
135
	private GdalDriver	                 	drv;
136
	private GdalDataset 							dstDataset = null;
137
	private GdalRasterBand 					rband = null;
138
	private GeoTransform 					geot = null; //Datos de georeferenciaci?n
139
	//private OGRSpatialReference 			oSRS; //Datos de proyecci?n
140
	private GdalBuffer[]					bufBands = null;
141
	private int 							nBlocks = 0; //N?mero de bloques en Y en el que se divide la imagen para escribir
142
	private int 							anchoResto = 0; //Tama?o del ?ltimo bloque de la imagen.
143
	private boolean							write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido
144
	private int 							dataType = RasterBuffer.TYPE_UNDEFINED;
145

  
146
	/**
147
	 * Carga los par?metros de este driver.
148
	 * @param ident
149
	 */
150
	public void loadParams(String ident) {
151
		WriteFileFormatFeatures wfff = (WriteFileFormatFeatures)pInfo.getFileFeature().get(ident);
152
		wfff.loadParams();
153
		driverParams = (ParamsImpl)wfff.getParams();
154
	}
155

  
156
	public String getProviderName() {
157
		return GdalProvider.NAME;
158
	}
159

  
160
	/**
161
	 * Constructor para la obtenci?n de par?metros del driver
162
	 * @param fileName
163
	 */
164
	public GdalWriter(String fileName) {
165
		ident = fileUtil.getExtensionFromFileName(fileName);
166
		driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName();
167

  
168
		loadParams(ident);
169
	}
170

  
171
	/**
172
	 * Constructor para salvar datos servidos por el cliente
173
	 * @param dataWriter       	Objeto servidor de datos para el driver de escritura
174
	 * @param outFileName      	Fichero de salida
175
	 * @param nBands            N?mero de bandas
176
	 * @param at                Transformaci?n afin
177
	 * @param outSizeX		  	Tama?o de salida en X
178
	 * @param outSizeY			Tama?o de salida en Y
179
	 * @param dataType			Tipo de dato
180
	 * @param params
181
	 * @param proj
182
	 * @throws GdalException
183
	 * @throws IOException
184
	 */
185
	public GdalWriter(	DataServerWriter dataWriter,
186
			String outFileName,
187
			Integer nBands,
188
			AffineTransform at,
189
			Integer outSizeX,
190
			Integer outSizeY,
191
			Integer dataType,
192
			Params params,
193
			IProjection proj) throws GdalException, IOException {
194
		this(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, new Boolean(true));
195
	}
196

  
197
	/**
198
	 * Constructor para salvar datos servidos por el cliente
199
	 * @param dataWriter       	Objeto servidor de datos para el driver de escritura
200
	 * @param outFileName
201
	 * @param nBands
202
	 * @param at
203
	 * @param outFilename      	Fichero de salida
204
	 * @param outSizeX		  	Tama?o de salida en X
205
	 * @param outSizeY			Tama?o de salida en Y
206
	 * @param dataType			Tipo de dato
207
	 * @param params
208
	 * @param proj
209
	 * @param geo				Flag que dice si se salva con georreferenciaci?n o sin ella
210
	 * @throws GdalException
211
	 * @throws IOException
212
	 */
213
	public GdalWriter(	DataServerWriter dataWriter,
214
			String outFileName,
215
			Integer nBands,
216
			AffineTransform at,
217
			Integer outSizeX,
218
			Integer outSizeY,
219
			Integer dataType,
220
			Params params,
221
			IProjection proj,
222
			Boolean geo)throws GdalException, IOException {
223

  
224
		this.proj = proj;
225
		ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1);
226
		driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName();
227
		this.dataType = dataType.intValue();
228
		this.at = at;
229
		percent = 0;
230

  
231
		this.dataWriter = dataWriter;
232
		this.outFileName = outFileName;
233

  
234
		this.sizeWindowX = outSizeX.intValue();
235
		this.sizeWindowY = outSizeY.intValue();
236

  
237
		if ((sizeWindowX < 0) || (sizeWindowY < 0))
238
			throw new IOException("Tama?o del fichero de salida erroneo.");
239

  
240
		this.nBands = nBands.intValue();
241

  
242
		//Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
243

  
244
		geot = new GeoTransform();
245
		geot.adfgeotransform[0] = at.getTranslateX();
246
		geot.adfgeotransform[3] = at.getTranslateY();
247
		geot.adfgeotransform[1] = at.getScaleX();
248
		geot.adfgeotransform[5] = at.getScaleY();
249
		geot.adfgeotransform[2] = at.getShearX();
250
		geot.adfgeotransform[4] = at.getShearY();
251

  
252
		String outRmf = outFileName.substring(0, outFileName.lastIndexOf("."));
253
		if(geo.booleanValue())
254
			rasterUtil.saveGeoInfo(outRmf, at, new Point2D.Double(sizeWindowX, sizeWindowY));
255

  
256
		if(params == null)
257
			loadParams(ident);
258
		else
259
			if(params instanceof ParamsImpl)
260
				this.driverParams = (ParamsImpl)params;
261

  
262
		init();
263
	}
264

  
265
	/**
266
	 * A?ade la proyecci?n Wkt con la que salvar.
267
	 * @param wkt
268
	 * @throws GdalException
269
	 */
270
	public void setWkt(String wkt) {
271
		if(dstDataset != null && wkt != null && wkt.compareTo("unknown") != 0)
272
			try {
273
				dstDataset.setProjection(wkt);
274
			} catch (GdalException e) {
275
				System.err.println("Proyecci?n Wkt no asignada en GdalWriter");
276
				return;
277
			}
278
	}
279

  
280
	/**
281
	 * Asigna el tipo de driver con el que se salvar? la imagen
282
	 * @param drvType        Tipo de driver
283
	 */
284
	public void setDriverType(String drvType) {
285
		this.driver = drvType;
286
	}
287

  
288
	/**
289
	 * Creaci?n del dataset de destino.
290
	 * @throws EcwException
291
	 */
292
	private void init() throws GdalException {
293
		//Controlamos que el tipo de driver sea correcto
294
		if (driver == null)
295
			throw new GdalException("Tipo de driver sin especificar.");
296

  
297
		boolean okdrvtype = false;
298

  
299
		String[] types = pInfo.getWriteDriversType();
300
		for (int i = 0; i < pInfo.getWriteNTypes(); i++)
301
			if (driver.equals(types[i]))
302
				okdrvtype = true;
303

  
304
		if (okdrvtype == false)
305
			throw new GdalException("El tipo de driver " + driver + " no est? soportado por GdalWriter.");
306

  
307
		//Obtenemos el driver y creamos el dataset del destino
308
		drv = GdalDataset.getDriverByName(driver);
309

  
310
		if (dstDataset != null) {
311
			dstDataset.close();
312
			dstDataset = null;
313
		}
314

  
315
		dstDataset = drv.create(outFileName, sizeWindowX, sizeWindowY,
316
				nBands, GdalDataSource.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams));
317

  
318
		dstDataset.setGeoTransform(geot);
319

  
320
		int blockSize = RasterLibrary.blockHeight;
321
		if(dataWriter.getBuffer() != null && dataWriter.getBuffer().isCached())
322
			blockSize = dataWriter.getBuffer().getBlockHeight();
323

  
324
		nBlocks = (sizeWindowY / blockSize);
325
		anchoResto = sizeWindowY - (nBlocks * blockSize);
326
	}
327

  
328
	/**
329
	 * @param fileName
330
	 * @throws GdalException
331
	 */
332
	public void anotherFile(String fileName) throws GdalException {
333
		dstDataset = drv.create(fileName, sizeWindowX, sizeWindowY,
334
				nBands, GdalDataSource.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams));
335
	}
336

  
337
	/**
338
	 * Convierte los par?metros obtenidos desde el objeto params a parametros
339
	 * comprensibles por la librer?a gdal
340
	 * @param p Params
341
	 * @return Array de par?metros
342
	 */
343
	public String[] gdalParamsFromRasterParams(Params p) {
344
		if (p == null)
345
			return null;
346
		ArrayList<String> paramList = new ArrayList<String>();
347
		ParamImpl phot = (ParamImpl)p.getParamById("photometric");
348
		if (phot != null)
349
			paramList.add("PHOTOMETRIC=" + phot.getList()[((Integer) phot.getDefaultValue()).intValue()]);
350
		ParamImpl inter = (ParamImpl)p.getParamById("interleave");
351
		if (inter != null)
352
			paramList.add("INTERLEAVE=" + inter.getList()[((Integer) inter.getDefaultValue()).intValue()]);
353
		ParamImpl comp = (ParamImpl)p.getParamById("compression");// GIF LZW, ...
354
		if (comp != null)
355
			paramList.add("COMPRESS=" + comp.getList()[((Integer) comp.getDefaultValue()).intValue()]);
356
		ParamImpl comp1 = (ParamImpl)p.getParamById("compress"); // HFA (YES, NO)
357
		if (comp1 != null)
358
			paramList.add("COMPRESS=" + comp1.getList()[((Integer) comp1.getDefaultValue()).intValue()]);
359
		ParamImpl rrd = (ParamImpl)p.getParamById("rrd");
360
		if (rrd != null)
361
			paramList.add("HFA_USE_RRD=" + rrd.getList()[((Integer) rrd.getDefaultValue()).intValue()]);
362
		ParamImpl mtw = (ParamImpl)p.getParamById("Mtw");
363
		if (mtw != null)
364
			paramList.add("MTW=" + mtw.getList()[((Integer) mtw.getDefaultValue()).intValue()]);
365
		ParamImpl tw = (ParamImpl)p.getParamById("Tile Width");
366
		if (tw != null)
367
			paramList.add("BLOCKXSIZE=" + tw.getList()[((Integer) tw.getDefaultValue()).intValue()]);
368
		ParamImpl th = (ParamImpl)p.getParamById("Tile Height");
369
		if (th != null)
370
			paramList.add("BLOCKYSIZE=" + th.getList()[((Integer) th.getDefaultValue()).intValue()]);
371
		ParamImpl qt = (ParamImpl)p.getParamById("quality");
372
		if (qt != null)
373
			paramList.add("QUALITY=" + qt.getDefaultValue());
374
		ParamImpl prog = (ParamImpl)p.getParamById("progressive");
375
		if (prog != null)
376
			paramList.add("PROGRESSIVE=" + prog.getDefaultValue());
377

  
378
		if (paramList.size() == 0)
379
			return null;
380

  
381
		String[] result = new String[paramList.size()];
382
		for (int i = 0; i < result.length; i++)
383
			result[i] = (String) paramList.get(i);
384
		return result;
385
	}
386

  
387
	/**
388
	 * Escritura de datos tipo Byte.
389
	 * @param sizeY Alto del bloque que se escribe.
390
	 * @param posicionY Posici?ny a partir desde donde se comienza.
391
	 */
392
	public void writeByteBand(int sizeY, int posicionY) {
393
		byte[][] buftmp = dataWriter.readByteData(sizeWindowX, sizeY);
394
		for(int iBand = 0; iBand < nBands; iBand ++)
395
			bufBands[iBand].buffByte = new byte[buftmp[iBand].length];
396

  
397
		//Escribimos el bloque destino
398
		for (int iBand = 0; iBand < buftmp.length; iBand++)
399
			for (int i = 0; i < buftmp[iBand].length; i++)
400
				bufBands[iBand].buffByte[i] = buftmp[iBand][i];
401

  
402
		for (int iBand = 0; iBand < buftmp.length; iBand++)
403
			try {
404
				rband = dstDataset.getRasterBand(iBand + 1);
405
				rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Byte);
406
				bufBands[iBand].buffByte = null;
407
			} catch (GdalException e) {
408
				//No se est? escribiendo ...
409
			}
410
		buftmp = null;
411

  
412
	}
413

  
414
	/**
415
	 * Escritura de datos tipo Short.
416
	 * @param sizeY Alto del bloque que se escribe.
417
	 * @param posicionY Posici?ny a partir desde donde se comienza.
418
	 */
419
	public void writeShortBand(int sizeY, int posicionY) {
420
		short[][] buftmp = dataWriter.readShortData(sizeWindowX, sizeY);
421
		for(int iBand = 0; iBand < nBands; iBand ++)
422
			bufBands[iBand].buffShort = new short[buftmp[iBand].length];
423

  
424
		//Escribimos el bloque destino
425
		for (int iBand = 0; iBand < nBands; iBand++)
426
			for (int i = 0; i < buftmp[iBand].length; i++)
427
				bufBands[iBand].buffShort[i] = buftmp[iBand][i];
428

  
429
		for (int iBand = 0; iBand < nBands; iBand++)
430
			try {
431
				rband = dstDataset.getRasterBand(iBand + 1);
432
				rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Int16);
433
				bufBands[iBand].buffShort = null;
434
			} catch (GdalException e) {
435
				//No se est? escribiendo ...
436
			}
437
		buftmp = null;
438
	}
439

  
440
	/**
441
	 * Escritura de datos tipo Int.
442
	 * @param sizeY Alto del bloque que se escribe.
443
	 * @param posicionY Posici?ny a partir desde donde se comienza.
444
	 */
445
	public void writeIntBand(int sizeY, int posicionY) {
446
		int[][] buftmp = dataWriter.readIntData(sizeWindowX, sizeY);
447
		for(int iBand = 0; iBand < nBands; iBand ++)
448
			bufBands[iBand].buffInt = new int[buftmp[iBand].length];
449

  
450
		//Escribimos el bloque destino
451
		for (int iBand = 0; iBand < buftmp.length; iBand++)
452
			for (int i = 0; i < buftmp[iBand].length; i++)
453
				bufBands[iBand].buffInt[i] = buftmp[iBand][i];
454

  
455
		for (int iBand = 0; iBand < buftmp.length; iBand++)
456
			try {
457
				rband = dstDataset.getRasterBand(iBand + 1);
458
				rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Int32);
459
				bufBands[iBand].buffInt = null;
460
			} catch (GdalException e) {
461
				//No se est? escribiendo ...
462
			}
463
		buftmp = null;
464
	}
465

  
466
	/**
467
	 * Escritura de datos tipo Float.
468
	 * @param sizeY Alto del bloque que se escribe.
469
	 * @param posicionY Posici?ny a partir desde donde se comienza.
470
	 */
471
	public void writeFloatBand(int sizeY, int posicionY) {
472
		float[][] buftmp = dataWriter.readFloatData(sizeWindowX, sizeY);
473
		for(int iBand = 0; iBand < nBands; iBand ++)
474
			bufBands[iBand].buffFloat = new float[buftmp[iBand].length];
475

  
476
		//Escribimos el bloque destino
477
		for (int iBand = 0; iBand < buftmp.length; iBand++)
478
			for (int i = 0; i < buftmp[iBand].length; i++)
479
				bufBands[iBand].buffFloat[i] = buftmp[iBand][i];
480

  
481
		for (int iBand = 0; iBand < buftmp.length; iBand++)
482
			try {
483
				rband = dstDataset.getRasterBand(iBand + 1);
484
				rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Float32);
485
				bufBands[iBand].buffFloat = null;
486
			} catch (GdalException e) {
487
				//No se est? escribiendo ...
488
			}
489
		buftmp = null;
490
	}
491

  
492
	/**
493
	 * Escritura de datos tipo Double.
494
	 * @param sizeY Alto del bloque que se escribe.
495
	 * @param posicionY Posici?ny a partir desde donde se comienza.
496
	 */
497
	public void writeDoubleBand(int sizeY, int posicionY) {
498
		double[][] buftmp = dataWriter.readDoubleData(sizeWindowX, sizeY);
499
		for(int iBand = 0; iBand < nBands; iBand ++)
500
			bufBands[iBand].buffDouble = new double[buftmp[iBand].length];
501

  
502
		//Escribimos el bloque destino
503
		for (int iBand = 0; iBand < buftmp.length; iBand++)
504
			for (int i = 0; i < buftmp[iBand].length; i++)
505
				bufBands[iBand].buffDouble[i] = buftmp[iBand][i];
506

  
507
		for (int iBand = 0; iBand < buftmp.length; iBand++)
508
			try {
509
				rband = dstDataset.getRasterBand(iBand + 1);
510
				rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Float64);
511
				bufBands[iBand].buffDouble = null;
512
			} catch (GdalException e) {
513
				//No se est? escribiendo ...
514
			}
515
		buftmp = null;
516
	}
517
	/**
518
	 * Escritura para tipo de dato ARGB.
519
	 * @param sizeY Alto del bloque que se escribe.
520
	 * @param posicionY Posici?ny a partir desde donde se comienza.
521
	 * @throws ProcessInterruptedException
522
	 * @throws OutOfMemoryError
523
	 */
524
	public void writeARGBBand(int sizeY, int posicionY)
525
			throws ProcessInterruptedException, OutOfMemoryError {
526
		int[] buftmp = dataWriter.readARGBData(sizeWindowX, sizeY, 0);
527
		for(int iBand = 0; iBand < nBands; iBand ++)
528
			bufBands[iBand].buffByte = new byte[buftmp.length];
529

  
530
		//Escribimos el bloque destino
531
		for (int i = 0; i < buftmp.length; i++) {
532
			bufBands[0].buffByte[i] = (byte) (((buftmp[i] & 0xff0000) >> 16) &
533
					0xff);
534
			bufBands[1].buffByte[i] = (byte) (((buftmp[i] & 0xff00) >> 8) & 0xff);
535
			bufBands[2].buffByte[i] = (byte) ((buftmp[i] & 0xff) & 0xff);
536
		}
537

  
538
		try {
539
			rband = dstDataset.getRasterBand(1);
540
			rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[0],
541
					GdalDataset.GDT_Byte);
542
			rband = dstDataset.getRasterBand(2);
543
			rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[1],
544
					GdalDataset.GDT_Byte);
545
			rband = dstDataset.getRasterBand(3);
546
			rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[2],
547
					GdalDataset.GDT_Byte);
548
		} catch (GdalException e) {
549
			e.printStackTrace();
550
		}
551

  
552
		bufBands[0].buffByte = null;
553
		bufBands[1].buffByte = null;
554
		bufBands[2].buffByte = null;
555
	}
556

  
557
	/**
558
	 * Escribe tres bandas en el GDALRasterBand desde el DataServerWriter con una
559
	 * altura definida por sizeY.
560
	 * @param buftmp        Buffer
561
	 * @param sizeY        Altura en pixels del bloque leido
562
	 * @param posicionY        Posici?n y a partir de la cual se escribe en el GDALRasterBand destino
563
	 */
564
	private void writeBands(int sizeY, int posicionY)
565
			throws ProcessInterruptedException, OutOfMemoryError {
566
		//leemos el bloque origen
567

  
568
		switch(dataType){
569
		case RasterBuffer.TYPE_IMAGE:
570
			writeARGBBand(sizeY, posicionY);
571
			break;
572
		case RasterBuffer.TYPE_BYTE:
573
			writeByteBand(sizeY, posicionY);
574
			break;
575
		case RasterBuffer.TYPE_SHORT:
576
			writeShortBand(sizeY, posicionY);
577
			break;
578
		case RasterBuffer.TYPE_INT:
579
			writeIntBand(sizeY, posicionY);
580
			break;
581
		case RasterBuffer.TYPE_FLOAT:
582
			writeFloatBand(sizeY, posicionY);
583
			break;
584
		case RasterBuffer.TYPE_DOUBLE:
585
			writeDoubleBand(sizeY, posicionY);
586
			break;
587
		}
588
	}
589

  
590
	/**
591
	 * Funci?n que gestiona la lectura desde el origen y la escritura
592
	 * de Gdal sobre el fichero destino.
593
	 * @param mode        Modo de escritura
594
	 * @throws IOException
595
	 */
596
	private void write(int mode) throws IOException, ProcessInterruptedException,
597
			OutOfMemoryError {
598
		RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
599
		bufBands = new GdalBuffer[nBands];
600
		for(int iBand = 0; iBand < nBands; iBand ++)
601
			bufBands[iBand] = new GdalBuffer();
602

  
603
		int blockSize = RasterLibrary.blockHeight;
604
		if(dataWriter instanceof DefaultDataServerWriter) {
605
			Buffer source = ((DefaultDataServerWriter)dataWriter).getSource();
606
			if(source.isCached()) {
607
				blockSize = source.getBlockHeight();
608
			}
609
		}
610

  
611
		percent = 0;
612
		nBlocks = (sizeWindowY / blockSize);
613
		double increment = (blockSize * 100) / (double)sizeWindowY;
614

  
615
		if (mode == DefaultRasterWriter.MODE_DATAWRITE)
616
			for (int iBlock = 0; iBlock < nBlocks; iBlock++) {
617
				if(task.getEvent() != null)
618
					task.manageEvent(task.getEvent());
619
				int posicionY = iBlock * blockSize;
620
				if(write)
621
					writeBands( blockSize, posicionY);
622
				percent = (int)((iBlock + 1) * increment);
623
			}
624

  
625
		if (anchoResto != 0)
626
			if (mode == DefaultRasterWriter.MODE_DATAWRITE) {
627
				if(task.getEvent() != null)
628
					task.manageEvent(task.getEvent());
629
				int posicionY = nBlocks * blockSize;
630
				if(write)
631
					writeBands(anchoResto, posicionY);
632
				percent = (int)(nBlocks * increment);
633
			}
634

  
635
	}
636

  
637
	/**
638
	 * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
639
	 * @throws IOException
640
	 */
641
	public void fileWrite() throws IOException, ProcessInterruptedException {
642
		write(DefaultRasterWriter.MODE_FILEWRITE);
643
	}
644

  
645
	/**
646
	 * Realiza una copia en el formato especificado.
647
	 * @param driverDst
648
	 * @param dst
649
	 * @param src
650
	 * @param bstrict
651
	 * @param params
652
	 * @throws IOException
653
	 * @throws GdalException
654
	 */
655
	public static void createCopy(GdalDriver driverDst, String dst, String src,
656
			boolean bstrict, String[] params) throws IOException, GdalException {
657
		if (dst == null || src == null)
658
			throw new IOException("No se ha asignado un fichero de entrada.");
659

  
660
		GdalProvider gdalFile;
661
		try {
662
			GdalDataParameters par = new GdalDataParameters();
663
			par.setFile(new File(src));
664
			gdalFile = new GdalProvider(par, null);
665
			GdalDataset dstDataset = driverDst.createCopy(dst, gdalFile.getNative(), bstrict, params);
666
			if(	dst.endsWith(".jpg") ||
667
				dst.endsWith(".jpeg") ||
668
				dst.endsWith(".png"))
669
				RasterLocator.getManager().getFileUtils().createWorldFile(dst, gdalFile.getExtent(), (int)gdalFile.getWidth(), (int)gdalFile.getHeight());
670
			gdalFile.close();
671
			dstDataset.close();
672
		} catch (NotSupportedExtensionException e) {
673
			e.printStackTrace();
674
		}
675
	}
676

  
677
	/**
678
	 * Realiza la escritura de datos con los datos que le pasa el cliente.
679
	 * @throws IOException
680
	 * @throws RmfSerializerException
681
	 */
682
	public void dataWrite() throws IOException, ProcessInterruptedException,
683
			OutOfMemoryError {
684
		if (dataWriter == null)
685
			throw new IOException("No se ha obtenido un objeto de entrada para la escritura valido.");
686

  
687
		write(DefaultRasterWriter.MODE_DATAWRITE);
688

  
689
		if (driverParams.getParamById("tfw") != null &&
690
				((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue() instanceof Boolean /*&&
691
				((Boolean) ((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue()).booleanValue() == true*/)
692
			if (at != null)
693
				fileUtil.createWorldFile(this.outFileName, at, sizeWindowX, sizeWindowY);
694

  
695
		if (colorInterp != null)
696
			try {
697
                RasterLocator.getManager().getProviderServices()
698
                    .saveObjectToRmfFile(outFileName, DataStoreColorInterpretation.class, colorInterp);
699
			} catch (RmfSerializerException e) {
700
				throw new IOException("No se ha podido guardar la interpretacion de color");
701
			}
702

  
703
		this.savePrjFile(new File(this.outFileName), proj);
704
	}
705

  
706
	public void writeClose() {
707
		try {
708
			if(bufBands != null) {
709
				for (int i = 0; i < bufBands.length; i++) {
710
					bufBands[i].buffAPalette = null;
711
					bufBands[i].buffBPalette = null;
712
					bufBands[i].buffByte = null;
713
					bufBands[i].buffShort = null;
714
					bufBands[i].buffInt = null;
715
					bufBands[i].buffFloat = null;
716
					bufBands[i].buffDouble = null;
717
				}
718
			}
719

  
720
			if(dstDataset != null)
721
				dstDataset.close();
722
		} catch (GdalException e) {
723
			e.printStackTrace();
724
		}
725
	}
726

  
727
	/**
728
	 * Cancela el salvado de datos.
729
	 */
730
	public void writeCancel() {
731
		write = false;
732
	}
733

  
734
	/**
735
	 * Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
736
	 *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
737
	 * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
738
	 * de salida.
739
	 * @return True si puede escribirse y false si no puede
740
	 */
741
	public boolean isWrite() {
742
		return write;
743
	}
744

  
745
	/**
746
	 * Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
747
	 *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
748
	 * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
749
	 * de salida.
750
	 * @param write Variable booleana. True si puede escribirse y false si no puede
751
	 */
752
	public void setWrite(boolean write) {
753
		this.write = write;
754
	}
755

  
756
	/**
757
	 * Asigna los par?metros del driver modificados por el cliente.
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff