Statistics
| Revision:

root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.lib / org.gvsig.proj.lib.api / src / main / java / org / cresques / coerce / CoerceToCRS.java @ 827

History | View | Annotate | Download (2.21 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.cresques.coerce;
24

    
25
import org.cresques.cts.IProjection;
26
import org.gvsig.fmap.crs.CRSFactory;
27
import org.gvsig.tools.dataTypes.CoercionException;
28
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
29

    
30
/**
31
 * Convert a string value of projection code to IProjection
32
 *
33
 * @author gvSIG Team
34
 * @version $Id$
35
 *
36
 */
37
public class CoerceToCRS implements Coercion {
38

    
39
    public Object coerce(Object value) throws CoercionException {
40
        if (value == null) {
41
            return null;
42
        }
43
        try {
44
            if (value instanceof IProjection) {
45
                return value;
46
            }
47
            Object proj = CRSFactory.getCRS(value.toString());
48
            if (proj == null) {
49
                throw new CoercionException("Can't convert value '" + getValueAsString(value) + "' to IProjection. Probably not a valid format for a projection.");
50
            }
51
            return proj;
52
        } catch (Exception e) {
53
            throw new CoercionException("Can't convert value '" + getValueAsString(value) + "' to IProjection. Probably not a valid format for a projection.", e);
54
        }
55

    
56
    }
57

    
58
    private String getValueAsString(Object value) {
59
        try {
60
            return value.toString();
61
        } catch (Exception e) {
62
            return "(unknow)";
63
        }
64
    }
65

    
66
}