gvsig-gdal / trunk / org.gvsig.gdal2 / org.gvsig.gdal2.prov / org.gvsig.gdal2.prov.raster / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalStoreProviderCreateCopy.java @ 370
History | View | Annotate | Download (3.62 KB)
1 |
package org.gvsig.raster.gdal.provider; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import org.cresques.cts.ICRSFactory; |
7 |
import org.cresques.cts.IProjection; |
8 |
import org.gdal.gdal.Dataset; |
9 |
import org.gdal.gdal.Driver; |
10 |
import org.gdal.gdal.gdal; |
11 |
import org.gvsig.fmap.dal.exception.CreateException; |
12 |
import org.gvsig.fmap.dal.exception.InitializeException; |
13 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
14 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
15 |
import org.gvsig.tools.dynobject.DynObject; |
16 |
|
17 |
/**
|
18 |
* Raster GDAL store provider that stores new raster through createCopy method
|
19 |
*
|
20 |
* @author dmartinezizquierdo
|
21 |
*
|
22 |
*/
|
23 |
public class RasterGdalStoreProviderCreateCopy extends DefaultRasterGdalStoreProvider { |
24 |
|
25 |
private final static String AUX_DRIVER_NAME = "MEM"; |
26 |
|
27 |
protected RasterGdalStoreProviderCreateCopy(RasterGdalFileStoreParameters params,
|
28 |
DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException {
|
29 |
super(params, storeServices, metadata);
|
30 |
} |
31 |
|
32 |
/**
|
33 |
* Constructor
|
34 |
*
|
35 |
* @param params
|
36 |
* @param storeServices
|
37 |
* @throws InitializeException
|
38 |
*/
|
39 |
public RasterGdalStoreProviderCreateCopy(RasterGdalFileStoreParameters params,
|
40 |
DataStoreProviderServices storeServices) throws InitializeException {
|
41 |
super(params, storeServices);
|
42 |
} |
43 |
|
44 |
protected RasterGdalStoreProviderCreateCopy(NewRasterGdalStoreParameters params, Driver gdalDriver) { |
45 |
super(params, gdalDriver);
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public void store(boolean overwrite) throws CreateException { |
50 |
NewRasterGdalStoreParameters newRasterParams = (NewRasterGdalStoreParameters) this.getParameters();
|
51 |
File file = newRasterParams.getFile();
|
52 |
Buffer buffer = newRasterParams.getBuffer();
|
53 |
|
54 |
String[] options = getStoringOptions(newRasterParams); |
55 |
|
56 |
if (file.exists() && !overwrite) {
|
57 |
throw new CreateException("File already exists: " + file.toString(), null); |
58 |
} |
59 |
Dataset gdalDataset = null;
|
60 |
// Provider driver doesn't support create
|
61 |
// So we create another raster with a driver that supports it
|
62 |
// and get its copy with our Provider driver
|
63 |
|
64 |
IProjection projection = buffer.getProjection(); |
65 |
try {
|
66 |
Driver auxDriver = gdal.GetDriverByName(AUX_DRIVER_NAME);
|
67 |
Dataset auxDataset = auxDriver.Create("", buffer.getColumns(), buffer.getRows(), buffer.getBandCount());
|
68 |
|
69 |
|
70 |
if (projection != null) { |
71 |
String wkt = projection.export(ICRSFactory.FORMAT_WKT_ESRI);
|
72 |
auxDataset.SetProjection(wkt); |
73 |
} |
74 |
auxDataset.SetGeoTransform(createGeoTransform(buffer)); |
75 |
|
76 |
fillDataset(buffer, auxDataset); |
77 |
|
78 |
if (options.length == 0) { |
79 |
gdalDataset = gdalDriver.CreateCopy(file.getAbsolutePath(), auxDataset, 1);
|
80 |
} else {
|
81 |
gdalDataset = gdalDriver.CreateCopy(file.getAbsolutePath(), auxDataset, 1, options);
|
82 |
} |
83 |
if(gdalDataset==null){ |
84 |
throw new CreateException("Can't create gdalDataset: '" + gdal.GetLastErrorMsg()+"'", null); |
85 |
} |
86 |
|
87 |
gdalDataset.FlushCache(); |
88 |
} finally {
|
89 |
if (gdalDataset != null) { |
90 |
gdalDataset.delete(); |
91 |
} |
92 |
} |
93 |
// Create WLD
|
94 |
try {
|
95 |
createWorldFile(file, buffer); |
96 |
} catch (IOException e) { |
97 |
throw new CreateException("WLD File", e); |
98 |
} |
99 |
|
100 |
// Create PRJ file
|
101 |
try {
|
102 |
savePrjFile(file, projection); |
103 |
} catch (IOException e) { |
104 |
throw new CreateException("PRJ File", e); |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
} |