View Javadoc
1   package fr.ifremer.dali.ui.swing.util.map;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.geotools.data.FileDataStore;
29  import org.geotools.data.FileDataStoreFinder;
30  import org.geotools.data.simple.SimpleFeatureSource;
31  import org.geotools.map.FeatureLayer;
32  import org.geotools.map.Layer;
33  import org.geotools.map.MapContent;
34  import org.geotools.styling.SLD;
35  import org.geotools.styling.Style;
36  import org.geotools.swing.JMapFrame;
37  import org.geotools.swing.action.SafeAction;
38  import org.geotools.swing.data.JFileDataStoreChooser;
39  import org.opengis.feature.simple.SimpleFeatureType;
40  import org.opengis.feature.type.FeatureType;
41  import org.opengis.feature.type.PropertyDescriptor;
42  
43  import javax.swing.*;
44  import java.awt.*;
45  import java.awt.event.ActionEvent;
46  import java.io.File;
47  import java.io.IOException;
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.Random;
51  
52  /**
53   * @author peck7 on 30/05/2017.
54   */
55  public class CartoTest {
56  
57      private JMapFrame frame;
58      private MapContent map;
59      private static final Log log = LogFactory.getLog(CartoTest.class);
60  
61      public static void main(String[] args) throws Exception {
62  
63          // customize frame
64          CartoTest test = new CartoTest();
65          test.displayFrame();
66      }
67  
68      private void displayFrame() {
69  
70          // Set up a MapContent with the two layers
71          map = new MapContent();
72          map.setTitle("ImageLab");
73  
74  
75          // Create a JMapFrame with a menu to choose the display style for the
76          frame = new JMapFrame(map);
77          frame.setSize(800, 600);
78          frame.enableStatusBar(true);
79          frame.enableToolBar(true);
80          frame.enableLayerTable(true);
81  
82          JMenuBar menuBar = new JMenuBar();
83          frame.setJMenuBar(menuBar);
84          JMenu menu = new JMenu("Layer");
85          menuBar.add(menu);
86  
87          menu.add( new SafeAction("Add layer") {
88              public void action(ActionEvent e) throws Throwable {
89                  Layer layer = addLayer();
90                  if (layer != null) {
91                      map.addLayer(layer);
92                      frame.repaint();
93                  }
94              }
95          });
96  
97          // Finally display the map frame.
98          // When it is closed the app will exit.
99          frame.setVisible(true);
100 
101     }
102 
103     private Layer addLayer() throws IOException {
104 
105         // display a data store file chooser dialog for shapefiles
106         File file = JFileDataStoreChooser.showOpenFile("shp", null);
107         if (file == null) {
108             return null;
109         }
110 
111         FileDataStore store = FileDataStoreFinder.getDataStore(file);
112         SimpleFeatureSource featureSource = store.getFeatureSource();
113 
114         // CachingFeatureSource is deprecated as experimental (not yet production ready)
115 //        CachingFeatureSource cache = new CachingFeatureSource(featureSource);
116 
117 //        Style style = SLD.createSimpleStyle(featureSource.getSchema());
118 //        Style style = SLD.createPolygonStyle(Color.BLACK, randomColor(), 1f, "name", null);
119         Style style = createStyle(featureSource.getSchema());
120         return new FeatureLayer(featureSource /*cache*/, style);
121     }
122 
123     private Style createStyle(FeatureType schema) {
124         if (schema instanceof SimpleFeatureType) {
125 
126             log.debug("schema descriptors :");
127             List<String> attr = new ArrayList<>();
128             for (PropertyDescriptor descriptor : schema.getDescriptors()) {
129                 attr.add(descriptor.getName().toString());
130             }
131             log.debug(attr);
132 
133             if (attr.contains("name")) {
134                 return SLD.createPolygonStyle(Color.BLACK, randomColor(), 1f, "name", null);
135             } else if (attr.contains("NAME")) {
136                 return SLD.createPolygonStyle(Color.BLACK, randomColor(), 1f, "NAME", null);
137             } else {
138                 return SLD.createPolygonStyle(Color.BLACK, randomColor(), 1f);
139             }
140         }
141 
142         return SLD.createSimpleStyle(schema);
143     }
144 
145     private Color randomColor() {
146         Random random = new Random();
147         return new Color(
148                 100 + random.nextInt(155),
149                 100 + random.nextInt(155),
150                 100 + random.nextInt(155)
151         );
152     }
153 }