View Javadoc
1   package nl.tudelft.simulation.dsol.animation.gis.map;
2   
3   import java.awt.Color;
4   import java.awt.geom.Point2D;
5   import java.awt.geom.Rectangle2D;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.djutils.draw.bounds.Bounds2d;
10  import org.djutils.logger.CategoryLogger;
11  
12  import nl.tudelft.simulation.dsol.animation.gis.FeatureInterface;
13  import nl.tudelft.simulation.dsol.animation.gis.GisObject;
14  import nl.tudelft.simulation.dsol.animation.gis.SerializablePath;
15  import nl.tudelft.simulation.language.d2.Shape;
16  
17  /**
18   * Feature contains an element of a layer, defined by a key value combination, with its own colors.<br>
19   * TODO: minimum scale and maximum scale to draw features has to be added again, but first, scale needs to be defined properly.
20   * <p>
21   * Copyright (c) 2021-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
23   * project is distributed under a three-clause BSD-style license, which can be found at
24   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
25   * </p>
26   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   */
28  public class Feature implements FeatureInterface
29  {
30      /** */
31      private static final long serialVersionUID = 20210201L;
32  
33      /** the key that defines a feature in a layer, can be "*" if no features are defined. */
34      private String key = "*";
35  
36      /**
37       * the value belonging to the key that defines the feature in a layer, can be "*" if all elements in the data source that
38       * have the correct key have to be drawn.
39       */
40      private String value = "*";
41  
42  //    /** the dataSource to use. */
43  //    private DataSourceInterface dataSource;
44  
45      /** the list of shapes that have been retrieved for this feature. */
46      private List<GisObject> shapes = new ArrayList<>();
47  
48      /** the fillColor of the layer, by default no fill. */
49      private Color fillColor = null;
50  
51      /** the outlineColor. */
52      private Color outlineColor = Color.BLACK;
53      
54      /** whether the shapes have been read or not. */
55      private boolean initialized = false;
56  
57      /** {@inheritDoc} */
58      @Override
59      public final String getKey()
60      {
61          return this.key;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public final void setKey(final String key)
67      {
68          this.key = key;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final String getValue()
74      {
75          return this.value;
76      }
77  
78  //    /** {@inheritDoc} */
79  //    @Override
80  //    public DataSourceInterface getDataSource()
81  //    {
82  //        return this.dataSource;
83  //    }
84  
85      /** {@inheritDoc} */
86      @Override
87      public boolean isInitialized()
88      {
89          return this.initialized;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void setInitialized(final boolean initialized)
95      {
96          this.initialized = initialized;
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public int getNumShapes()
102     {
103         return this.shapes.size();
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public GisObject getShape(final int index) throws IndexOutOfBoundsException
109     {
110         return this.shapes.get(index);
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public List<GisObject> getShapes()
116     {
117         return this.shapes;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public List<GisObject> getShapes(final Bounds2d rectangle)
123     {
124         List<GisObject> result = new ArrayList<>();
125         Rectangle2D rectangle2D = rectangle.toRectangle2D();
126         for (GisObject shape : this.shapes)
127         {
128             if (shape.getShape() instanceof SerializablePath)
129             {
130                 if (Shape.overlaps(rectangle2D, ((SerializablePath) shape.getShape()).getBounds2D()))
131                 {
132                     result.add(shape);
133                 }
134             }
135             else if (shape.getShape() instanceof Point2D)
136             {
137                 if (rectangle2D.contains((Point2D) shape.getShape()))
138                 {
139                     result.add(shape);
140                 }
141             }
142             else
143             {
144                 CategoryLogger.always().error("unknown shape in cached content " + shape);
145             }
146         }
147         return result;
148     }
149 
150 //    /** {@inheritDoc} */
151 //    @Override
152 //    public void setDataSource(final DataSourceInterface dataSource)
153 //    {
154 //        this.dataSource = dataSource;
155 //    }
156 
157     /** {@inheritDoc} */
158     @Override
159     public final void setValue(final String value)
160     {
161         this.value = value;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public Color getFillColor()
167     {
168         return this.fillColor;
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     public void setFillColor(final Color fillColor)
174     {
175         this.fillColor = fillColor;
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public Color getOutlineColor()
181     {
182         return this.outlineColor;
183     }
184 
185     /** {@inheritDoc} */
186     @Override
187     public void setOutlineColor(final Color outlineColor)
188     {
189         this.outlineColor = outlineColor;
190     }
191 
192 }