svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / PolygonPlacementParallel.java @ 40911
History | View | Annotate | Download (5.86 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.labeling.placements; |
42 |
|
43 |
import java.awt.Rectangle; |
44 |
import java.awt.geom.PathIterator; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Vector; |
48 |
|
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
51 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
52 |
import org.gvsig.fmap.geom.GeometryLocator; |
53 |
import org.gvsig.fmap.geom.primitive.Point; |
54 |
import org.gvsig.fmap.geom.type.GeometryType; |
55 |
import org.gvsig.fmap.mapcontext.ViewPort; |
56 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
57 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
58 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics; |
59 |
import org.gvsig.tools.task.Cancellable; |
60 |
import org.slf4j.Logger; |
61 |
import org.slf4j.LoggerFactory; |
62 |
|
63 |
public class PolygonPlacementParallel implements ILabelPlacement{ |
64 |
|
65 |
private static final Logger logger = LoggerFactory.getLogger( |
66 |
PolygonPlacementParallel.class); |
67 |
|
68 |
public ArrayList<LabelLocationMetrics> guess( |
69 |
ILabelClass lc, Geometry geom, |
70 |
IPlacementConstraints placementConstraints, |
71 |
double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
|
72 |
|
73 |
if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES; |
74 |
|
75 |
double theta = 0; |
76 |
Point start_po = null; |
77 |
|
78 |
try {
|
79 |
if(placementConstraints.isFitInsidePolygon()){
|
80 |
start_po = geom.getInteriorPoint(); |
81 |
} else {
|
82 |
start_po = geom.centroid(); |
83 |
} |
84 |
} catch (Exception exc) { |
85 |
logger.error("While getting centroid/interior point.", exc);
|
86 |
} |
87 |
|
88 |
Point2D startingPoint = new Point2D.Double(start_po.getX(), start_po.getY()); |
89 |
|
90 |
// calculated with the Linear Regression technique
|
91 |
PathIterator pi = geom.getPathIterator(null); |
92 |
Rectangle geomBounds = geom.getBounds();
|
93 |
double sumx = 0, sumy = 0, sumxx = 0, sumyy = 0, sumxy = 0; |
94 |
double Sxx, Sxy, b, a;
|
95 |
double[] coords = new double[6]; |
96 |
int count = 0; |
97 |
|
98 |
// add points to the regression process
|
99 |
Vector<Point2D> v = new Vector<Point2D>(); |
100 |
while (!pi.isDone()) {
|
101 |
pi.currentSegment(coords); |
102 |
Point2D p;
|
103 |
if (geomBounds.width > geomBounds.height)
|
104 |
p = new Point2D.Double(coords[0], coords[1]); |
105 |
else
|
106 |
p = new Point2D.Double(coords[1], coords[0]); |
107 |
v.addElement(p); |
108 |
count++; |
109 |
sumx += p.getX(); |
110 |
sumy += p.getY(); |
111 |
sumxx += p.getX()*p.getX(); |
112 |
sumyy += p.getY()*p.getY(); |
113 |
sumxy += p.getX()*p.getY(); |
114 |
pi.next(); |
115 |
} |
116 |
|
117 |
// start regression
|
118 |
double n = (double) count; |
119 |
Sxx = sumxx-sumx*sumx/n; |
120 |
Sxy = sumxy-sumx*sumy/n; |
121 |
b = Sxy/Sxx; |
122 |
a = (sumy-b*sumx)/n; |
123 |
|
124 |
boolean isVertical = false; |
125 |
if (geomBounds.width < geomBounds.height) {
|
126 |
if (b == 0) { |
127 |
// force vertical (to avoid divide by zero)
|
128 |
isVertical = true;
|
129 |
|
130 |
} else {
|
131 |
// swap axes
|
132 |
double bAux = 1/b; |
133 |
a = - a / b; |
134 |
b = bAux; |
135 |
} |
136 |
} |
137 |
|
138 |
if (isVertical){
|
139 |
theta = AbstractLinePlacement.HALF_PI; |
140 |
} else {
|
141 |
double p1x = 0; |
142 |
double p1y =geomBounds.height-a;
|
143 |
double p2x = geomBounds.width;
|
144 |
double p2y = geomBounds.height-
|
145 |
(a+geomBounds.width*b); |
146 |
|
147 |
theta = -Math.atan(((p2y - p1y) / (p2x - p1x)) );
|
148 |
} |
149 |
|
150 |
ArrayList<LabelLocationMetrics> guessed = new ArrayList<LabelLocationMetrics>(); |
151 |
Rectangle labelBounds = lc.getBounds();
|
152 |
double cosTheta = Math.cos(theta); |
153 |
double sinTheta = Math.sin(theta); |
154 |
double halfHeight = labelBounds.getHeight()*0.5; |
155 |
double halfWidth= labelBounds.getWidth()*0.5; |
156 |
double offsetX = halfHeight * sinTheta + halfWidth*cosTheta;
|
157 |
double offsetY = -halfHeight * cosTheta + halfWidth*sinTheta;
|
158 |
double offsetRX=vp.toMapDistance((int)offsetX); |
159 |
double offsetRY=vp.toMapDistance((int)offsetY); |
160 |
startingPoint.setLocation(startingPoint.getX() - offsetRX, |
161 |
startingPoint.getY() - offsetRY); |
162 |
|
163 |
Point auxp = null; |
164 |
|
165 |
try {
|
166 |
auxp = GeometryLocator.getGeometryManager().createPoint( |
167 |
startingPoint.getX(),startingPoint.getY(), SUBTYPES.GEOM2D); |
168 |
} catch (Exception e) { |
169 |
logger.error("While creating point.", e);
|
170 |
} |
171 |
|
172 |
auxp.transform(vp.getAffineTransform()); |
173 |
guessed.add(new LabelLocationMetrics(
|
174 |
new Point2D.Double(auxp.getX(),auxp.getY()), -theta, true)); |
175 |
return guessed;
|
176 |
} |
177 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
178 |
int shapeType) {
|
179 |
|
180 |
GeometryType gt = null;
|
181 |
try {
|
182 |
gt = GeometryLocator.getGeometryManager().getGeometryType( |
183 |
shapeType, SUBTYPES.GEOM2D); |
184 |
} catch (Exception e) { |
185 |
logger.error("While getting geo type.", e);
|
186 |
} |
187 |
|
188 |
if (gt.isTypeOf(TYPES.SURFACE) || shapeType == TYPES.MULTISURFACE) {
|
189 |
return placementConstraints.isParallel();
|
190 |
} |
191 |
return false; |
192 |
} |
193 |
|
194 |
} |