Statistics
| Revision:

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 / AbstractLinePlacement.java @ 40911

History | View | Annotate | Download (4.94 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.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45
import java.util.ArrayList;
46

    
47
import org.apache.batik.ext.awt.geom.PathLength;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.mapcontext.ViewPort;
50
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
51
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics;
53
import org.gvsig.tools.task.Cancellable;
54

    
55
/**
56
 *
57
 * AbstractLinePlacement.java
58
 *
59
 *
60
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
61
 *
62
 */
63
public abstract class AbstractLinePlacement implements ILabelPlacement {
64
        public static final double PI = Math.PI;
65
        public static final double HALF_PI = PI * 0.5;
66
        private ArrayList<LabelLocationMetrics> guessed =
67
                        new ArrayList<LabelLocationMetrics>();
68
        private static final double TOLERANCE = 1E-2;
69

    
70

    
71
        public ArrayList<LabelLocationMetrics> guess(ILabelClass lc, Geometry geom,
72
                        IPlacementConstraints pc, double cartographicSymbolSize,
73
                        Cancellable cancel, ViewPort vp) {
74
                
75
                guessed.clear();
76

    
77
                Geometry shp = geom.cloneGeometry();
78
                shp.transform(vp.getAffineTransform());
79

    
80
                PathLength pathLen = new PathLength(shp);
81

    
82
                LabelLocationMetrics initial = initialLocation(lc, pc, pathLen, cancel);
83

    
84
                if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES;
85

    
86
                double theta = initial.getRotation();
87

    
88

    
89
                double xOffset = 0;
90
                double yOffset = 0;
91
                Rectangle2D bounds = lc.getBounds();
92

    
93
                if (pc.isPageOriented()) {
94
                        if(theta >  HALF_PI) { // from origin to left-down
95
                                theta = (theta - Math.PI);
96

    
97
                                Point2D anchor = initial.getAnchor();
98
//                                double cosTheta = Math.cos(theta);
99
                                double sinTheta = Math.sin(theta);
100
                                double width = bounds.getWidth();
101
                                double height = bounds.getHeight()*1.1;
102
                                initial.getAnchor().setLocation(
103
                                                anchor.getX()+ (sinTheta*width /* + cosTheta*height*/),
104
                                                anchor.getY()- (/*cosTheta*width*/ + sinTheta*height));
105
                        } else if (theta < -HALF_PI ) { // from origin to left-up
106
                                if (Math.abs(Math.abs(theta) - HALF_PI) -TOLERANCE > 0)
107
                                        // the condition avoids errors when segment is almost up-down vertical
108
                                {
109
                                        theta = (theta + Math.PI);
110
                                        Point2D anchor = initial.getAnchor();
111
                                        double cosTheta = Math.cos(theta);
112
                                        double sinTheta = Math.sin(theta);
113
                                        double width = bounds.getWidth();
114
                                        double height = bounds.getHeight()*1.1;
115
                                        initial.getAnchor().setLocation(
116
                                                        anchor.getX()- (cosTheta*width/* + sinTheta*height*/),
117
                                                        anchor.getY()- (sinTheta*width + cosTheta*height));
118
                                }
119
                        }
120

    
121

    
122
                }
123

    
124
                if (pc.isBelowTheLine() ||
125
                                pc.isOnTheLine() ||
126
                                pc.isAboveTheLine()) {
127

    
128
                        double h = bounds.getHeight()*0.5;
129

    
130
                        xOffset += h * Math.sin(theta);
131
                        yOffset += h * Math.cos(theta);
132

    
133

    
134
                        Point2D initialAnchor = initial.getAnchor();
135
                        // calculate the possibles in the inverse
136
                        // order to the preferred order and add it
137
                        // always in the first position
138
                        if (pc.isBelowTheLine()) {
139
                                Point2D anchor = new Point2D.Double(
140
                                                initialAnchor.getX() +        -xOffset,
141
                                                initialAnchor.getY() +        +yOffset);
142
                                guessed.add(0, new LabelLocationMetrics(anchor, theta, false));
143
                        }
144

    
145
                        if (pc.isOnTheLine()) {
146
                                guessed.add(0, new LabelLocationMetrics(initial.getAnchor(), theta, false));
147
                        }
148

    
149
                        if (pc.isAboveTheLine()) {
150
                                Point2D anchor = new Point2D.Double(
151
                                                initialAnchor.getX() +        +xOffset,
152
                                                initialAnchor.getY() +  -yOffset);
153
                                guessed.add(0, new LabelLocationMetrics(anchor, theta, false));
154
                        }
155
                } else {
156
                        // will say that on the line is legal
157
                        guessed.add(0, initial);
158
                }
159
                return guessed;
160
        }
161

    
162
        abstract LabelLocationMetrics initialLocation(
163
                        ILabelClass lc, IPlacementConstraints pc,
164
                        PathLength pathLen, Cancellable cancel);
165

    
166
}