Bonjour ! je travail sur un projet de géolocalisation sur les données et la carte de mon pays, mais je rencontre quelques pb avec l'affichage des points. En fait j'ai pu récupérer sur le site de CARPE une carte (shp) des routes et un fichier texte des coordonnées (longitude, latitude) de certaines villes; j'ai suivi quelsques tuto de la doc officielle de Geotools (j'utilise la version 2.6-M1 et les codes des docs sont pour la version 2.5, mais je me débrouille) pour créer un shapefile à partir de ces coordonnées (issus d'un fichier texte ou csv). Lorsque j'affiche les routes et les la carte quej'ai crée dans QGIS, les deux couches se supperposent normalement comme il se doit; mais lorsque je le fait dans mon code, les points représentant les villes s'affiche carrément hors de la carte des routes. Je me demande si le problème peut être lié au "System Coordinate" utilisé par chacun des shapefiles?
Voici le code que j'utilise pour créer le shape file:
Et ic le code utilisé pour afficher les points sur le MapPane:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static void main(String[] args) throws Exception { File file = getCSVFile(args); final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point,name:String"); // see createFeatureType(); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection(); BufferedReader reader = new BufferedReader( new FileReader( file )); try { String line = reader.readLine(); System.out.println( "Header: "+ line ); GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null); for( line = reader.readLine(); line != null; line = reader.readLine()){ String split[] = line.split("\\,"); double longitude = Double.parseDouble( split[1] ); double latitude = Double.parseDouble( split[0] ); String name = split[2]; name = name.replace("é", "é"); Point point = factory.createPoint( new Coordinate(longitude,latitude)); SimpleFeature feature = SimpleFeatureBuilder.build( TYPE, new Object[]{point, name}, null ); collection.add( feature ); } } finally { reader.close(); } File newFile = getNewShapeFile( file ); DataStoreFactorySpi factory = new ShapefileDataStoreFactory(); Map<String,Serializable> create = new HashMap<String,Serializable>(); create.put("url", newFile.toURI().toURL() ); create.put("create spatial index",Boolean.TRUE); ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore( create ); newDataStore.createSchema( TYPE ); newDataStore.forceSchemaCRS( DefaultGeographicCRS.WGS84 ); Transaction transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; FeatureStore<SimpleFeatureType, SimpleFeature> featureStore; featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore.getFeatureSource( typeName ); featureStore.setTransaction(transaction); try { featureStore.addFeatures(collection); transaction.commit(); } catch (Exception problem){ problem.printStackTrace(); transaction.rollback(); } finally { transaction.close(); } System.exit(0); // we are actually exiting because we will use a Swing JFileChooser } ... static SimpleFeatureType createFeatureType(){ SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName( "Location" ); builder.setCRS( DefaultGeographicCRS.WGS84 ); //add attributes in order builder.add( "Location", Point.class ); builder.length(15).add( "Name", String.class ); //build the type final SimpleFeatureType LOCATION = builder.buildFeatureType(); return LOCATION; }
P.S: Je n'ai pas préférer me lancer dans GeoToolKit faute de docs; dc s'il ya une piste ce serait vraiment gentil de la rendre disponible.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public void addLayer(){ try{ File file = promptShapeFile(new String[0]); if(file == null) return; System.out.println("File loaded: "+file.getName()+" at location: "+file.getAbsolutePath()); System.out.println("Building the new layer..."); infoLabel.setText("Buildig the new layer..."); shapeURL = file.toURI().toURL(); store = new ShapefileDataStore(shapeURL); String name = store.getTypeNames()[0]; source = store.getFeatureSource(name); System.out.println("System coordinates :"+ source.getSchema().getCoordinateReferenceSystem()); StyleBuilder sb = new StyleBuilder(); LineSymbolizer ls2 = sb.createLineSymbolizer(Color.ORANGE, 2); Style newStyle = sb.createStyle(); newStyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(ls2)); /** Pour représenter mes points j'utilise plutot le style suivant: * *StyleBuilder sb = new StyleBuilder(); *Style newStyle = sb.createStyle(); *System.out.println("System coordinates :"+ * collection.getSchema().getCoordinateReferenceSystem()); *Mark circle = sb.createMark(StyleBuilder.MARK_CIRCLE, Color.BLUE); *Graphic graph2 = sb.createGraphic(null, circle, null, 1, 4, 0); *PointSymbolizer pointSymbolizer = sb.createPointSymbolizer(graph2); *newStyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(pointSymbolizer)); * */ MapLayer maplayer = new DefaultMapLayer(collection, newStyle); MapLayer maplayer = new DefaultMapLayer(source, newStyle); maplayer.setTitle(file.getName()); maplayer.setVisible(true); maplayer.setQuery(Query.ALL); mapcontext.addLayer(maplayer); getLayersVector().addElement(maplayer); showedLayers = null; DefaultListModel model = (DefaultListModel) layersDetails.getLayersList().getModel(); model.addElement(new MyMapLayer(maplayer, true, mapcontext.getLayerCount()-1)); // Refreshing the map viewer System.out.println("Refreshing the map viewer..."); infoLabel.setText("Refreshing the map viewer..."); StreamingRenderer render = new StreamingRenderer(); mappane.setRenderer(render); mappane.setMapArea(mapcontext.getLayerBounds()); }catch(Exception e){ System.out.println("Cannot add the new layer to the map!"); infoLabel.setText("Cannot add the new layer to the map!"); e.printStackTrace(); } finally{ infoLabel.setText("..."); } }
Merci d'avance!
Partager