Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / DissolveRule.java @ 1852

History | View | Annotate | Download (3.14 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.dissolve;
25

    
26
import java.util.Date;
27
import java.util.HashMap;
28
import java.util.Iterator;
29

    
30
import com.vividsolutions.jts.geom.Geometry;
31

    
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.tools.util.CompareUtils;
34

    
35
/**
36
 * Decides if dissolve two features based in an alphanumeric criteria:
37
 * two features will be dissolved if they have the same value for
38
 * a given (and single) field.
39
 */
40
public class DissolveRule implements IDissolveRule {
41
        protected String                    nameField           = null;
42
        private HashMap<String, String>  funcMap              = null;
43
        
44
        public DissolveRule(String nameField, HashMap<String, String> funcMap) {
45
                this.nameField = nameField;
46
                this.funcMap = funcMap;
47
        }
48
        
49
        /*
50
         * (non-Javadoc)
51
         * @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#verifyIfDissolve(com.vividsolutions.jts.geom.Geometry, com.vividsolutions.jts.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
52
         */
53
        public boolean verifyIfDissolve(Geometry g1, Geometry g2, Feature f1, Feature f2) {
54
                Object obj1 = f1.get(nameField);
55
                Object obj2 = f2.get(nameField);
56
                return compareTo(obj1, obj2);
57
        }
58
        
59
        /*
60
         * (non-Javadoc)
61
         * @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#isFunctionIncluded(java.lang.String)
62
         */
63
        public boolean isFunctionIncluded(String func) {
64
                Iterator<String> it = funcMap.values().iterator();
65
                while (it.hasNext()) {
66
                        if(it.next().compareTo(func) == 0)
67
                                return true;
68
                }
69
                return false;
70
        }
71
        
72
    /**
73
     * Compares two fields if both have the same data type
74
     *
75
     * @param obj1
76
     * @param obj2
77
     * @return
78
     */
79
    private boolean compareTo(Object obj1, Object obj2) {
80
        if (obj1 instanceof Comparable && obj2 instanceof Comparable) {
81
            return CompareUtils.compare((Comparable) obj1, (Comparable) obj2)==0;
82
        }
83
        return false;
84
    }
85

    
86
        /*
87
         * (non-Javadoc)
88
         * @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#getField(java.lang.String)
89
         */
90
        public String getFieldName(String func) {
91
                return funcMap.get(func);
92
        }
93
        
94
        /*
95
         * (non-Javadoc)
96
         * @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#getIndexField()
97
         */
98
        public String getFieldName() {
99
                return nameField;
100
        }
101

    
102
}
103