svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / labeling / placements / PolygonPlacementParallel.java @ 19091
History | View | Annotate | Download (4.81 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.symbology.fmap.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 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
50 |
|
51 |
import com.iver.cit.gvsig.fmap.core.FShape; |
52 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
53 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints; |
54 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass; |
55 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelLocationMetrics; |
56 |
import com.iver.utiles.swing.threads.Cancellable; |
57 |
import com.vividsolutions.jts.geom.Geometry; |
58 |
import com.vividsolutions.jts.geom.Point; |
59 |
|
60 |
public class PolygonPlacementParallel implements ILabelPlacement{ |
61 |
|
62 |
public ArrayList<LabelLocationMetrics> guess(LabelClass lc, FShape shp, |
63 |
IPlacementConstraints placementConstraints, |
64 |
double cartographicSymbolSize, Cancellable cancel) {
|
65 |
|
66 |
if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES; |
67 |
|
68 |
Geometry geo = FConverter.java2d_to_jts(shp); |
69 |
double theta = 0; |
70 |
if (geo == null) { |
71 |
return CannotPlaceLabel.NO_PLACES;
|
72 |
} |
73 |
|
74 |
Point pJTS = geo.getCentroid();
|
75 |
|
76 |
if (pJTS == null) { |
77 |
Logger.getAnonymousLogger().log(Level.SEVERE, "no centroid could be found"); |
78 |
return CannotPlaceLabel.NO_PLACES;
|
79 |
} |
80 |
|
81 |
Point2D startingPoint = new Point2D.Double(pJTS.getX(), pJTS.getY()); |
82 |
|
83 |
// calculated with the Linear Regression technique
|
84 |
PathIterator pi = shp.getPathIterator(null); |
85 |
Rectangle geomBounds = shp.getBounds();
|
86 |
double sumx = 0, sumy = 0, sumxx = 0, sumyy = 0, sumxy = 0; |
87 |
double Sxx, Sxy, b, a;
|
88 |
double[] coords = new double[6]; |
89 |
int count = 0; |
90 |
|
91 |
// add points to the regression process
|
92 |
Vector<Point2D> v = new Vector<Point2D>(); |
93 |
while (!pi.isDone()) {
|
94 |
pi.currentSegment(coords); |
95 |
Point2D p;
|
96 |
if (geomBounds.width > geomBounds.height)
|
97 |
p = new Point2D.Double(coords[0], coords[1]); |
98 |
else
|
99 |
p = new Point2D.Double(coords[1], coords[0]); |
100 |
v.addElement(p); |
101 |
count++; |
102 |
sumx += p.getX(); |
103 |
sumy += p.getY(); |
104 |
sumxx += p.getX()*p.getX(); |
105 |
sumyy += p.getY()*p.getY(); |
106 |
sumxy += p.getX()*p.getY(); |
107 |
pi.next(); |
108 |
} |
109 |
|
110 |
// start regression
|
111 |
double n = (double) count; |
112 |
Sxx = sumxx-sumx*sumx/n; |
113 |
Sxy = sumxy-sumx*sumy/n; |
114 |
b = Sxy/Sxx; |
115 |
a = (sumy-b*sumx)/n; |
116 |
|
117 |
boolean isVertical = false; |
118 |
if (geomBounds.width < geomBounds.height) {
|
119 |
if (b == 0) { |
120 |
// force vertical (to avoid divide by zero)
|
121 |
isVertical = true;
|
122 |
|
123 |
} else {
|
124 |
// swap axes
|
125 |
double bAux = 1/b; |
126 |
a = - a / b; |
127 |
b = bAux; |
128 |
} |
129 |
} |
130 |
|
131 |
if (isVertical){
|
132 |
theta = AbstractLinePlacement.HALF_PI; |
133 |
} else {
|
134 |
double p1x = 0; |
135 |
double p1y =geomBounds.height-a;
|
136 |
double p2x = geomBounds.width;
|
137 |
double p2y = geomBounds.height-
|
138 |
(a+geomBounds.width*b); |
139 |
|
140 |
theta = -Math.atan(((p2y - p1y) / (p2x - p1x)) );
|
141 |
} |
142 |
|
143 |
ArrayList<LabelLocationMetrics> guessed = new ArrayList<LabelLocationMetrics>(); |
144 |
Rectangle labelBounds = lc.getBounds();
|
145 |
double cosTheta = Math.cos(theta); |
146 |
double sinTheta = Math.sin(theta); |
147 |
double halfHeight = labelBounds.getHeight()*0.5; |
148 |
double halfWidth= labelBounds.getWidth()*0.5; |
149 |
double offsetX = - halfHeight * sinTheta + halfWidth*cosTheta;
|
150 |
double offsetY = halfHeight * cosTheta + halfWidth*sinTheta;
|
151 |
startingPoint.setLocation(startingPoint.getX() - offsetX, |
152 |
startingPoint.getY() - offsetY); |
153 |
guessed.add(new LabelLocationMetrics(startingPoint, theta, true)); |
154 |
return guessed;
|
155 |
} |
156 |
|
157 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
158 |
int shapeType) {
|
159 |
if (shapeType == FShape.POLYGON) {
|
160 |
return placementConstraints.isParallel();
|
161 |
} |
162 |
return false; |
163 |
} |
164 |
|
165 |
} |