Revision 4182 org.gvsig.raster.gdal/trunk/org.gvsig.raster.gdal/org.gvsig.raster.gdal.io/src/main/java/org/gvsig/raster/gdal/util/DefaultCRSUtils.java

View differences:

DefaultCRSUtils.java
23 23

  
24 24
import org.cresques.cts.ICRSFactory;
25 25
import org.cresques.cts.IProjection;
26
import org.gdal.osr.SpatialReference;
27

  
26 28
import org.gvsig.fmap.crs.CRSFactory;
27 29
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
28
import org.gvsig.jogr.CrsGdalException;
29
import org.gvsig.jogr.OGRException;
30
import org.gvsig.jogr.OGRSpatialReference;
31
import org.slf4j.LoggerFactory;
30

  
32 31
/**
33 32
 * Esta clase se encarga de hacer la conversion entre Wkt e IProjection.
34 33
 *
35
 * El uso se hace mediante dos llamadas estaticas que son: convertIProjectionToWkt y
34
 * El uso se hace mediante dos llamadas estaticas que son:
35
 * convertIProjectionToWkt y
36 36
 * convertWktToIProjection.
37 37
 *
38
 * Antes de usarlos, hay que saber si tenemos acceso a gvSIG e intentar coger el factory desde all?.
39
 * Ya que su uso consume tiempo y espacio de memoria y es preferible reaprovechar ese objeto ya
38
 * Antes de usarlos, hay que saber si tenemos acceso a gvSIG e intentar coger el
39
 * factory desde all?.
40
 * Ya que su uso consume tiempo y espacio de memoria y es preferible
41
 * reaprovechar ese objeto ya
40 42
 * creado. Esto se hace con setCRSFactory.
41 43
 *
42
 * En caso de no asignarse el ya existente, el crear? uno interno y lo dejara en una variable estatica.
44
 * En caso de no asignarse el ya existente, el crear? uno interno y lo dejara en
45
 * una variable estatica.
43 46
 *
44 47
 * @version 11/07/2008
45 48
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
46 49
 */
47 50
public class DefaultCRSUtils implements CRSUtils {
48
	private ICRSFactory factory = null;
49 51

  
50
	public void setCRSFactory(ICRSFactory factory) {
51
		this.factory = factory;
52
	}
52
    private ICRSFactory factory = null;
53 53

  
54
	/**
55
	 * Devuelve el CRSFactory asignado desde fuera o creado desde dentro, todo depende de como se haya
56
	 * usado.
57
	 *
58
	 * @param code
59
	 * @return
60
	 */
61
	private IProjection getCRS(String code) {
62
		if (factory == null)
63
			factory = CRSFactory.getCRSFactory(); //new ProjectionPool();
64
		return factory.get(code);
65
	}
54
    public void setCRSFactory(ICRSFactory factory) {
55
        this.factory = factory;
56
    }
66 57

  
67
	public IProjection convertWktToIProjection(String wkt) {
68
		if (wkt == null || wkt.equals(""))
69
			return null;
58
    /**
59
     * Devuelve el CRSFactory asignado desde fuera o creado desde dentro, todo
60
     * depende de como se haya
61
     * usado.
62
     *
63
     * @param code
64
     * @return
65
     */
66
    private IProjection getCRS(String code) {
67
        if (factory == null)
68
            factory = CRSFactory.getCRSFactory(); // new ProjectionPool();
69
        return factory.get(code);
70
    }
70 71

  
71
		String code = null;
72
		String name = null;
72
    public IProjection convertWktToIProjection(String wkt) {
73
        if (wkt == null || wkt.equals(""))
74
            return null;
73 75

  
74
		OGRSpatialReference oSRSSource = new OGRSpatialReference();
75
		try {
76
			OGRSpatialReference.importFromWkt(oSRSSource, wkt);
76
        String code = null;
77
        String name = null;
77 78

  
78
			code = oSRSSource.getAuthorityCode("PROJCS");
79
			if (code == null)
80
				code = oSRSSource.getAuthorityCode("GEOGCS");
81
			name = oSRSSource.getAuthorityName("PROJCS");
82
			if (name == null)
83
				name = oSRSSource.getAuthorityName("GEOGCS");
84
			try {
85
				if(name != null && code != null)
86
					return getCRS(name + ":" + code);
87
			} catch (NumberFormatException ex) {
88
				return null;
89
			}
90
		} catch (OGRException e) {
91
			LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
92
		} catch (CrsGdalException e) {
93
			LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo EPSG", e);
94
		}
79
        SpatialReference oSRSSource = new SpatialReference();
95 80

  
96
		return null;
97
	}
81
        oSRSSource.ImportFromWkt(wkt);
98 82

  
99
	public String convertIProjectionToWkt(IProjection projection) {
100
		if(projection == null)
101
			return null;
102
		OGRSpatialReference oSRSSource = new OGRSpatialReference();
83
        code = oSRSSource.GetAuthorityCode("PROJCS");
84
        if (code == null)
85
            code = oSRSSource.GetAuthorityCode("GEOGCS");
86
        name = oSRSSource.GetAuthorityName("PROJCS");
87
        if (name == null)
88
            name = oSRSSource.GetAuthorityName("GEOGCS");
89
        try {
90
            if (name != null && code != null)
91
                return getCRS(name + ":" + code);
92
        } catch (NumberFormatException ex) {
93
            return null;
94
        }
103 95

  
104
		String code = projection.getAbrev();
105
		String name = code.substring(0, code.indexOf(":"));
106
		code = code.substring(code.indexOf(":") + 1);
96
        return null;
97
    }
107 98

  
108
		try {
109
			do
110
				if (name.equals("EPSG")) {
111
					OGRSpatialReference.importFromEPSG(oSRSSource, Integer.parseInt(code));
112
					break;
113
				}
114
			while (false);
115
			return OGRSpatialReference.exportToWkt(oSRSSource);
116
		} catch (CrsGdalException e) {
117
			LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
118
		} catch (NumberFormatException e) {
119
			LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
120
		} catch (OGRException e) {
121
			LoggerFactory.getLogger(getClass().getName()).debug("Problemas obteniendo el c?digo WKT", e);
122
		}
123
		return null;
124
	}
99
    public String convertIProjectionToWkt(IProjection projection) {
100
        if (projection == null)
101
            return null;
102
        SpatialReference oSRSSource = new SpatialReference();
103

  
104
        String code = projection.getAbrev();
105
        String name = code.substring(0, code.indexOf(":"));
106
        code = code.substring(code.indexOf(":") + 1);
107

  
108
        do
109
            if (name.equals("EPSG")) {
110
                oSRSSource.ImportFromEPSG(Integer.parseInt(code));
111
                break;
112
            }
113
        while (false);
114
        return oSRSSource.ExportToWkt();
115
    }
125 116
}

Also available in: Unified diff