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 @ 1259
History | View | Annotate | Download (3.74 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 |
|
34 |
/**
|
35 |
* Decides if dissolve two features based in an alphanumeric criteria:
|
36 |
* two features will be dissolved if they have the same value for
|
37 |
* a given (and single) field.
|
38 |
*/
|
39 |
public class DissolveRule implements IDissolveRule { |
40 |
protected String nameField = null; |
41 |
private HashMap<String, String> funcMap = null; |
42 |
|
43 |
public DissolveRule(String nameField, HashMap<String, String> funcMap) { |
44 |
this.nameField = nameField;
|
45 |
this.funcMap = funcMap;
|
46 |
} |
47 |
|
48 |
/*
|
49 |
* (non-Javadoc)
|
50 |
* @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#verifyIfDissolve(com.vividsolutions.jts.geom.Geometry, com.vividsolutions.jts.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
51 |
*/
|
52 |
public boolean verifyIfDissolve(Geometry g1, Geometry g2, Feature f1, Feature f2) { |
53 |
Object obj1 = f1.get(nameField);
|
54 |
Object obj2 = f2.get(nameField);
|
55 |
return compareTo(obj1, obj2);
|
56 |
} |
57 |
|
58 |
/*
|
59 |
* (non-Javadoc)
|
60 |
* @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#isFunctionIncluded(java.lang.String)
|
61 |
*/
|
62 |
public boolean isFunctionIncluded(String func) { |
63 |
Iterator<String> it = funcMap.values().iterator(); |
64 |
while (it.hasNext()) {
|
65 |
if(it.next().compareTo(func) == 0) |
66 |
return true; |
67 |
} |
68 |
return false; |
69 |
} |
70 |
|
71 |
/**
|
72 |
* Compares two fields if both have the same data type
|
73 |
* @param obj1
|
74 |
* @param obj2
|
75 |
* @return
|
76 |
*/
|
77 |
private boolean compareTo(Object obj1, Object obj2) { |
78 |
if(obj1 instanceof String) { |
79 |
if(((String)obj1).compareTo((String)obj2) == 0) |
80 |
return true; |
81 |
} else if(obj1 instanceof Double) { |
82 |
if(((Double)obj1).compareTo((Double)obj2) == 0) |
83 |
return true; |
84 |
} else if(obj1 instanceof Integer) { |
85 |
if(((Integer)obj1).compareTo((Integer)obj2) == 0) |
86 |
return true; |
87 |
} else if(obj1 instanceof Boolean) { |
88 |
if(((Boolean)obj1).booleanValue() == ((Boolean)obj2).booleanValue()) |
89 |
return true; |
90 |
} else if(obj1 instanceof Float) { |
91 |
if(((Float)obj1).compareTo((Float)obj2) == 0) |
92 |
return true; |
93 |
} else if(obj1 instanceof Long) { |
94 |
if(((Long)obj1).compareTo((Long)obj2) == 0) |
95 |
return true; |
96 |
} else if(obj1 instanceof Byte) { |
97 |
if(((Byte)obj1).compareTo((Byte)obj2) == 0) |
98 |
return true; |
99 |
} else if(obj1 instanceof Date) { |
100 |
if(((Date)obj1).compareTo((Date)obj2) == 0) |
101 |
return true; |
102 |
} |
103 |
return false; |
104 |
} |
105 |
|
106 |
/*
|
107 |
* (non-Javadoc)
|
108 |
* @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#getField(java.lang.String)
|
109 |
*/
|
110 |
public String getFieldName(String func) { |
111 |
return funcMap.get(func);
|
112 |
} |
113 |
|
114 |
/*
|
115 |
* (non-Javadoc)
|
116 |
* @see org.gvsig.geoprocess.algorithm.dissolve.IDissolveCriteria#getIndexField()
|
117 |
*/
|
118 |
public String getFieldName() { |
119 |
return nameField;
|
120 |
} |
121 |
|
122 |
} |
123 |
|