1 package fr.ifremer.dali.ui.swing.util.map;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
64 CartoTest test = new CartoTest();
65 test.displayFrame();
66 }
67
68 private void displayFrame() {
69
70
71 map = new MapContent();
72 map.setTitle("ImageLab");
73
74
75
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
98
99 frame.setVisible(true);
100
101 }
102
103 private Layer addLayer() throws IOException {
104
105
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
115
116
117
118
119 Style style = createStyle(featureSource.getSchema());
120 return new FeatureLayer(featureSource , 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 }