gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.dissolve / src / main / java / org / gvsig / sextante / app / algorithm / dissolve / DissolveRule.java @ 172
History | View | Annotate | Download (3.63 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*/
|
20 |
package org.gvsig.sextante.app.algorithm.dissolve; |
21 |
|
22 |
import java.util.Date; |
23 |
import java.util.HashMap; |
24 |
import java.util.Iterator; |
25 |
|
26 |
import com.vividsolutions.jts.geom.Geometry; |
27 |
|
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
|
30 |
/**
|
31 |
* Decides if dissolve two features based in an alphanumeric criteria:
|
32 |
* two features will be dissolved if they have the same value for
|
33 |
* a given (and single) field.
|
34 |
*/
|
35 |
public class DissolveRule implements IDissolveRule { |
36 |
protected int indexField = -1; |
37 |
private HashMap<String, String> funcMap = null; |
38 |
|
39 |
public DissolveRule(int indexField, HashMap<String, String> funcMap) { |
40 |
this.indexField = indexField;
|
41 |
this.funcMap = funcMap;
|
42 |
} |
43 |
|
44 |
/*
|
45 |
* (non-Javadoc)
|
46 |
* @see org.gvsig.sextante.app.algorithm.dissolve.IDissolveCriteria#verifyIfDissolve(com.vividsolutions.jts.geom.Geometry, com.vividsolutions.jts.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
47 |
*/
|
48 |
public boolean verifyIfDissolve(Geometry g1, Geometry g2, Feature f1, Feature f2) { |
49 |
Object obj1 = f1.get(indexField);
|
50 |
Object obj2 = f2.get(indexField);
|
51 |
return compareTo(obj1, obj2);
|
52 |
} |
53 |
|
54 |
/*
|
55 |
* (non-Javadoc)
|
56 |
* @see org.gvsig.sextante.app.algorithm.dissolve.IDissolveCriteria#isFunctionIncluded(java.lang.String)
|
57 |
*/
|
58 |
public boolean isFunctionIncluded(String func) { |
59 |
Iterator<String> it = funcMap.values().iterator(); |
60 |
while (it.hasNext()) {
|
61 |
if(it.next().compareTo(func) == 0) |
62 |
return true; |
63 |
} |
64 |
return false; |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Compares two fields if both have the same data type
|
69 |
* @param obj1
|
70 |
* @param obj2
|
71 |
* @return
|
72 |
*/
|
73 |
private boolean compareTo(Object obj1, Object obj2) { |
74 |
if(obj1 instanceof String) { |
75 |
if(((String)obj1).compareTo((String)obj2) == 0) |
76 |
return true; |
77 |
} else if(obj1 instanceof Double) { |
78 |
if(((Double)obj1).compareTo((Double)obj2) == 0) |
79 |
return true; |
80 |
} else if(obj1 instanceof Integer) { |
81 |
if(((Integer)obj1).compareTo((Integer)obj2) == 0) |
82 |
return true; |
83 |
} else if(obj1 instanceof Boolean) { |
84 |
if(((Boolean)obj1).booleanValue() == ((Boolean)obj2).booleanValue()) |
85 |
return true; |
86 |
} else if(obj1 instanceof Float) { |
87 |
if(((Float)obj1).compareTo((Float)obj2) == 0) |
88 |
return true; |
89 |
} else if(obj1 instanceof Long) { |
90 |
if(((Long)obj1).compareTo((Long)obj2) == 0) |
91 |
return true; |
92 |
} else if(obj1 instanceof Byte) { |
93 |
if(((Byte)obj1).compareTo((Byte)obj2) == 0) |
94 |
return true; |
95 |
} else if(obj1 instanceof Date) { |
96 |
if(((Date)obj1).compareTo((Date)obj2) == 0) |
97 |
return true; |
98 |
} |
99 |
return false; |
100 |
} |
101 |
|
102 |
/*
|
103 |
* (non-Javadoc)
|
104 |
* @see org.gvsig.sextante.app.algorithm.dissolve.IDissolveCriteria#getField(java.lang.String)
|
105 |
*/
|
106 |
public String getFieldName(String func) { |
107 |
return funcMap.get(func);
|
108 |
} |
109 |
|
110 |
/*
|
111 |
* (non-Javadoc)
|
112 |
* @see org.gvsig.sextante.app.algorithm.dissolve.IDissolveCriteria#getIndexField()
|
113 |
*/
|
114 |
public int getIndexField() { |
115 |
return indexField;
|
116 |
} |
117 |
|
118 |
} |
119 |
|