IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SIG : Système d'information Géographique Discussion :

[Geotools] Manipuler les fichier ShapeFile


Sujet :

SIG : Système d'information Géographique

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2009
    Messages
    30
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 30
    Par défaut [Geotools] Manipuler les fichier ShapeFile
    Bonjour,

    Je suis novice dans la manipulation des Fichiers ShapeFile.j'ai pu trouvé sur le net des codes Java qui m'ont permis de charger les fichiers ShapeFile dans les vues de mon application développer avec éclipse RCP.
    le problème au quelle je suis confronté actuellement réside dans le fait que lors du chargement du shapfile j'aimerai ajouter des points de coordonnées.
    quelqu'un saurait t'il comment s'y prendre.
    Aussi, pour les logiciels de carto-graphie y'a t'il un moyen de développer deçu et ensuite intégrer le code dans mon application?
    Merci Pour vos réponse.

    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    package  views.map;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    import java.io.File;
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.swing.JButton;
    import javax.swing.JToolBar;
    
    import org.geotools.data.FileDataStore;
    import org.geotools.data.FileDataStoreFinder;
    import org.geotools.data.simple.SimpleFeatureCollection;
    import org.geotools.data.simple.SimpleFeatureIterator;
    import org.geotools.data.simple.SimpleFeatureSource;
    import org.geotools.factory.CommonFactoryFinder;
    import org.geotools.geometry.jts.ReferencedEnvelope;
    import org.geotools.map.DefaultMapContext;
    import org.geotools.map.MapContext;
    import org.geotools.styling.FeatureTypeStyle;
    import org.geotools.styling.Fill;
    import org.geotools.styling.Graphic;
    import org.geotools.styling.Mark;
    import org.geotools.styling.Rule;
    import org.geotools.styling.Stroke;
    import org.geotools.styling.Style;
    import org.geotools.styling.StyleFactory;
    import org.geotools.styling.Symbolizer;
    import org.geotools.swing.JMapFrame;
    import org.geotools.swing.data.JFileDataStoreChooser;
    import org.geotools.swing.event.MapMouseEvent;
    import org.geotools.swing.tool.CursorTool;
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.type.GeometryDescriptor;
    import org.opengis.filter.Filter;
    import org.opengis.filter.FilterFactory2;
    import org.opengis.filter.identity.FeatureId;
    
    import com.vividsolutions.jts.geom.LineString;
    import com.vividsolutions.jts.geom.MultiLineString;
    import com.vividsolutions.jts.geom.MultiPolygon;
    import com.vividsolutions.jts.geom.Polygon;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URI;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.awt.SWT_AWT;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.ui.part.ViewPart;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    import org.jfree.experimental.chart.swt.ChartComposite;
    
    public class View_RXLFULL_Map extends ViewPart {
    	public static final String ID = "S.view_rxl_map"; 
        private StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);
        private FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
    
        public static Integer fichierNMFId;
       
        private enum GeomType { POINT, LINE, POLYGON };
    
       
        private static final Color LINE_COLOUR = Color.BLUE;
        private static final Color FILL_COLOUR = Color.RED;//Couleur du ShapeFile
        private static final Color SELECTED_COLOUR = Color.YELLOW;
        private static final float OPACITY = 1.0f;
        private static final float LINE_WIDTH = 1.0f;
        private static final float POINT_SIZE = 10.0f;
    
        private JMapFrame mapFrame;
        private SimpleFeatureSource featureSource;
    
        private String geometryAttributeName;
        private GeomType geometryType;
        public View_RXLFULL_Map(){
        	
        	
        }
    
        public void displayShapefile(Composite parent,Graphics g) throws Exception {
        	File file = new File("C:/Documents and Settings/countries.shp");
        	
            FileDataStore store = FileDataStoreFinder.getDataStore(file);
            System.out.println("********"+store.getInfo());
            featureSource = store.getFeatureSource();
            setGeometry();
    
            MapContext map = new DefaultMapContext();
            
            map.setTitle("Feature selection tool example");
            Style style = createDefaultStyle();
            map.addLayer(featureSource, style);
            mapFrame = new JMapFrame(map);
            mapFrame.enableToolBar(true);
            mapFrame.enableStatusBar(true);
    
            JToolBar toolBar = mapFrame.getToolBar();
            JButton btn = new JButton("Select");
            toolBar.addSeparator();
            toolBar.add(btn);
    
            btn.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    mapFrame.getMapPane().setCursorTool(
                            new CursorTool() {
    
                                @Override
                                public void onMouseClicked(MapMouseEvent ev) {
                                    selectFeatures(ev);
                                }
                            });
                }
            });
    
       
            mapFrame.setSize(600, 600);
            mapFrame.setVisible(false);
            Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
    		java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
    		
    	
    		frame.add(mapFrame.getContentPane());
    		
        }
    
        void selectFeatures(MapMouseEvent ev) {
    
            System.out.println("Mouse click at: " + ev.getMapPosition());
    
            Point screenPos = ev.getPoint();
            Rectangle screenRect = new Rectangle(screenPos.x-2, screenPos.y-2, 5, 5);
            
              AffineTransform screenToWorld = mapFrame.getMapPane().getScreenToWorldTransform();
            Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
            ReferencedEnvelope bbox = new ReferencedEnvelope(
                    worldRect,
                    mapFrame.getMapContext().getCoordinateReferenceSystem());
    
       
            Filter filter = ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));
    
    
            try {
                SimpleFeatureCollection selectedFeatures =
                        featureSource.getFeatures(filter);
    
                SimpleFeatureIterator iter = selectedFeatures.features();
                Set<FeatureId> IDs = new HashSet<FeatureId>();
                try {
                    while (iter.hasNext()) {
                        SimpleFeature feature = iter.next();
                        IDs.add(feature.getIdentifier());
    
                        System.out.println("   " + feature.getIdentifier());
                    }
    
                } finally {
                    iter.close();
                }
    
                if (IDs.isEmpty()) {
                    System.out.println("   no feature selected");
                }
    
                displaySelectedFeatures(IDs);
    
            } catch (Exception ex) {
                ex.printStackTrace();
                return;
            }
        }
    
        public void displaySelectedFeatures(Set<FeatureId> IDs) {
            Style style;
    
            if (IDs.isEmpty()) {
                style = createDefaultStyle();
    
            } else {
                style = createSelectedStyle(IDs);
            }
            
            mapFrame.getMapContext().getLayer(0).setStyle(style);
            mapFrame.getMapPane().repaint();
        }
    
        private Style createDefaultStyle() {
            Rule rule = createRule(LINE_COLOUR, FILL_COLOUR);
    
            FeatureTypeStyle fts = sf.createFeatureTypeStyle();
            fts.rules().add(rule);
    
            Style style = sf.createStyle();
            style.featureTypeStyles().add(fts);
            return style;
        }
    
        private Style createSelectedStyle(Set<FeatureId> IDs) {
            Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
            selectedRule.setFilter(ff.id(IDs));
    
            Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
            otherRule.setElseFilter(true);
    
            FeatureTypeStyle fts = sf.createFeatureTypeStyle();
            fts.rules().add(selectedRule);
            fts.rules().add(otherRule);
    
            Style style = sf.createStyle();
            style.featureTypeStyles().add(fts);
            return style;
        }
    
        private Rule createRule(Color outlineColor, Color fillColor) {
            Symbolizer symbolizer = null;
            Fill fill = null;
            Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));
    
            switch (geometryType) {
                case POLYGON:
                    fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));
                    symbolizer = sf.createPolygonSymbolizer(stroke, fill, geometryAttributeName);
                    break;
    
                case LINE:
                    symbolizer = sf.createLineSymbolizer(stroke, geometryAttributeName);
                    break;
    
                case POINT:
                    fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));
    
                    Mark mark = sf.getCircleMark();
                    mark.setFill(fill);
                    mark.setStroke(stroke);
    
                    Graphic graphic = sf.createDefaultGraphic();
                    graphic.graphicalSymbols().clear();
                    graphic.graphicalSymbols().add(mark);
                    graphic.setSize(ff.literal(POINT_SIZE));
    
                    symbolizer = sf.createPointSymbolizer(graphic, geometryAttributeName);
            }
    
            Rule rule = sf.createRule();
            rule.symbolizers().add(symbolizer);
            return rule;
        }
    
        private void setGeometry() {
            GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
            geometryAttributeName = geomDesc.getLocalName();
    
            Class<?> clazz = geomDesc.getType().getBinding();
    
            if (Polygon.class.isAssignableFrom(clazz) ||
                    MultiPolygon.class.isAssignableFrom(clazz)) {
                geometryType = GeomType.POLYGON;
    
            } else if (LineString.class.isAssignableFrom(clazz) ||
                    MultiLineString.class.isAssignableFrom(clazz)) {
    
                geometryType = GeomType.LINE;
    
            } else {
                geometryType = GeomType.POINT;
            }
    
        }
    
    	public void createPartControl(Composite parent) {
    		
    				try {
    					
    					displayShapefile(parent);
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    		
    				
    				
    			
    	}
    
    	public void setFocus() {
    	}
    }

  2. #2
    Rédacteur
    Avatar de eclesia
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    2 113
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 113
    Par défaut
    Citation Envoyé par salivie
    Bonjour,
    je m'adresse a vous,en votre qualité de "Messie" des cartes Géo-référencée sur le forum de développez.
    En effet,hier j'ai fait un post sur le forum et j'ai toujours pas eu de réponse.
    je vous invite (vous prie)sur votre temps vous le permet a réagir sur la question.
    http://www.developpez.net/forums/d94...ier-shapefile/
    Merci pour l'attention que vous accordez a ma demande.
    Cordialement Salivie.
    Flatteur

    Je suis désolé mais cela fait un peu plus de deux ans maintenant que je n'utilise plus Geotools. (J'ai quitté leur equipe de developpement pour rejoindre GeotoolKit). Si je me rappelle bien il faut passer par un FeatureWriter sur le datastore alors qu'un "add" fait l'affaire sur geotoolkit.


    Aussi, pour les logiciels de carto-graphie y'a t'il un moyen de développer deçu et ensuite intégrer le code dans mon application?
    La seule application en SWT dont j'ai connaissance est UDIG. Sinon il y a plusieurs outil qui propose des possibilités de plugin en Swing : gvSIG et open jump on leur propre systeme. ou Puzzle-GIS sur Netbeans RCP avec la librairie GeotoolKit.

    Udig semble plus dans l'axe de ce que tu recherches. Encore une fois cela fait 4ans que j'ai quitté eclipse pour netbeans

    On a fait des choix bien opposé, désolé ne pas pouvoir t'aider plus.

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2009
    Messages
    30
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 30
    Par défaut
    Bonjour,
    D'abord je tiens à vous remercier d'avoir pris le soin de me répondre( Vous êtes la seule personne à l'avoir fait 'je pleurs de joies').
    Je pense que je vais m'orienter vers GeotoolKit pour continuer mon travail(ça fait 3 semaine que je bloque sur ce point avec GeoTools).
    Je vous tiendrai au courant de mon avancement sur le forum.Comme nous l'a dit un jour notre professeur(Serge Miranda) : <Le monde appartient à ceux qui partagent l'information> .
    Merci mille Fois!!
    Cordialement Salivie.

  4. #4
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2009
    Messages
    30
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 30
    Par défaut
    Bonjour,
    Finalement j'ai pu désigner mes coordonnées sur le Shapefile Avec Geotools.
    je mets à votre disposition le code que j'ai réalisé, il est toujours pas fini(couleur des coordonnées...).
    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    
    import javax.persistence.EntityManager;
    import javax.swing.JButton;
    import javax.swing.JToolBar;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.awt.SWT_AWT;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.ui.part.ViewPart;
    import org.geotools.data.DefaultTransaction;
    import org.geotools.data.FeatureStore;
    import org.geotools.data.FileDataStore;
    import org.geotools.data.FileDataStoreFinder;
    import org.geotools.data.simple.SimpleFeatureCollection;
    import org.geotools.data.simple.SimpleFeatureIterator;
    import org.geotools.data.simple.SimpleFeatureSource;
    import org.geotools.factory.CommonFactoryFinder;
    import org.geotools.feature.FeatureCollection;
    import org.geotools.feature.FeatureCollections;
    import org.geotools.feature.simple.SimpleFeatureBuilder;
    import org.geotools.geometry.jts.ReferencedEnvelope;
    import org.geotools.map.DefaultMapContext;
    import org.geotools.map.MapContext;
    import org.geotools.map.MapLayer;
    import org.geotools.styling.FeatureTypeStyle;
    import org.geotools.styling.Fill;
    import org.geotools.styling.Graphic;
    import org.geotools.styling.Mark;
    import org.geotools.styling.Rule;
    import org.geotools.styling.Stroke;
    import org.geotools.styling.Style;
    import org.geotools.styling.StyleFactory;
    import org.geotools.styling.Symbolizer;
    import org.geotools.swing.JMapFrame;
    import org.geotools.swing.event.MapMouseEvent;
    import org.geotools.swing.tool.CursorTool;
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.simple.SimpleFeatureType;
    import org.opengis.feature.type.AttributeDescriptor;
    import org.opengis.feature.type.GeometryDescriptor;
    import org.opengis.filter.Filter;
    import org.opengis.filter.FilterFactory2;
    import org.opengis.filter.identity.FeatureId;
    
    import sofrecom.model.MaquetteEntityManagerFactory;
    import sofrecom.model.cellule;
    import sofrecom.service.Rxl_Cellule_Service;
    import views.table.View_RXL_CELLULE_LEV_FULL_Table;
    
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.LineString;
    import com.vividsolutions.jts.geom.MultiLineString;
    import com.vividsolutions.jts.geom.MultiPolygon;
    import com.vividsolutions.jts.geom.Polygon;
    
    public class MAP extends ViewPart {
    	public static EntityManager em;
    	public static final String ID = "levfull_map"; 
        private StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);
        private FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
    
        public static  Integer fichierNMFId;
       
        private enum GeomType { POINT, LINE, POLYGON };
    
        
        private static final Color LINE_COLOUR = Color.BLUE;;
        private static final Color FILL_COLOUR = Color.lightGray;//Couleur du ShapeFile
        private static final Color SELECTED_COLOUR = Color.YELLOW;
        private static final float OPACITY = 1.0f;
        private static final float LINE_WIDTH = 5.0f;
        private static final float POINT_SIZE = 10.0f;
    
        private JMapFrame mapFrame;
        private SimpleFeatureSource featureSource;
    
        private String geometryAttributeName;
        private GeomType geometryType;
        
        public MAP(){    	
        	
        }
        public void addcoordinate(){
        List<cellule> liste_full = new ArrayList<cellule>();
    	
    	
    	em = MaquetteEntityManagerFactory.createEntityManager();
    	Rxl_Cellule_Service celluleservice = new Rxl_Cellule_Service(em);
    	
    	liste_full=celluleservice.rxlevfullmap(fichierNMFId);
    	
    	
    	
    	Iterator it=liste_full.iterator() ;
    	Coordinate[] coord=new Coordinate[liste_full.size()];
    	List<Coordinate> coord_list = new ArrayList<Coordinate>(); 
    	int i=0; 
    	Float[] latitude=new Float[liste_full.size()];
    	Float[] longitude=new Float[liste_full.size()];
    	 while(it.hasNext()){
    		 cellule cell = (cellule)it.next();
    		
    		 longitude[i]=cell.getRxl().getGps().getLongitude();
    		 latitude[i]=cell.getRxl().getGps().getLatitude();
    		 
    		 i++;
    	 }
    	
    	new MapUtil(longitude,latitude);
      }
        
        public void displayShapefile(Composite parent) throws Exception {
        	addcoordinate();
        	File file = new File("C:/countries.shp");
        	System.out.println("rxl   : "+file);
            FileDataStore store = FileDataStoreFinder.getDataStore(file);
            System.out.println("********"+store.getInfo());
            featureSource = store.getFeatureSource();
            setGeometry();
    
            MapContext map = new DefaultMapContext();
            
            map.setTitle("Feature selection tool example");
            Style style = createDefaultStyle();
            map.addLayer(featureSource, style);
            mapFrame = new JMapFrame(map);
            mapFrame.enableToolBar(true);
            mapFrame.enableStatusBar(true);
    
            JToolBar toolBar = mapFrame.getToolBar();
            JButton btn = new JButton("Select");
            toolBar.addSeparator();
            toolBar.add(btn);
    
            btn.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    mapFrame.getMapPane().setCursorTool(
                            new CursorTool() {
    
                                @Override
                                public void onMouseClicked(MapMouseEvent ev) {
                                    selectFeatures(ev);
                                }
                            });
                }
            });
            
            mapFrame.setSize(600, 600);
            mapFrame.setVisible(false);
            Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
    		java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
    		
    		frame.add(mapFrame.getContentPane());
    		
        }
    
        void selectFeatures(MapMouseEvent ev) {
    
            System.out.println("Mouse click at: " + ev.getMapPosition());
    
            Point screenPos = ev.getPoint();
            Rectangle screenRect = new Rectangle(screenPos.x-2, screenPos.y-2, 5, 5);
           
            AffineTransform screenToWorld = mapFrame.getMapPane().getScreenToWorldTransform();
            Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
            ReferencedEnvelope bbox = new ReferencedEnvelope(
                    worldRect,
                    mapFrame.getMapContext().getCoordinateReferenceSystem());
    
            Filter filter = ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));
    
            try {
                SimpleFeatureCollection selectedFeatures =
                        featureSource.getFeatures(filter);
    
                SimpleFeatureIterator iter = selectedFeatures.features();
                Set<FeatureId> IDs = new HashSet<FeatureId>();
                try {
                    while (iter.hasNext()) {
                        SimpleFeature feature = iter.next();
                        IDs.add(feature.getIdentifier());
    
                        System.out.println("   " + feature.getIdentifier());
                    }
    
                } finally {
                    iter.close();
                }
    
                if (IDs.isEmpty()) {
                    System.out.println("   no feature selected");
                }
    
                displaySelectedFeatures(IDs);
    
            } catch (Exception ex) {
                ex.printStackTrace();
                return;
            }
        }
    
        public void displaySelectedFeatures(Set<FeatureId> IDs) {
            Style style;
    
            if (IDs.isEmpty()) {
                style = createDefaultStyle();
    
            } else {
                style = createSelectedStyle(IDs);
            }
           
            mapFrame.getMapContext().getLayer(0).setStyle(style);
            mapFrame.getMapPane().repaint();
            
            
        }
    
        private Style createDefaultStyle() {
            Rule rule = createRule(LINE_COLOUR, FILL_COLOUR);
    
            FeatureTypeStyle fts = sf.createFeatureTypeStyle();
            fts.rules().add(rule);
    
            Style style = sf.createStyle();
            style.featureTypeStyles().add(fts);
            return style;
        }
    
        private Style createSelectedStyle(Set<FeatureId> IDs) {
            Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
            selectedRule.setFilter(ff.id(IDs));
    
            Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
            otherRule.setElseFilter(true);
    
            FeatureTypeStyle fts = sf.createFeatureTypeStyle();
            fts.rules().add(selectedRule);
            fts.rules().add(otherRule);
    
            Style style = sf.createStyle();
            style.featureTypeStyles().add(fts);
            return style;
        }
    
        private Rule createRule(Color outlineColor, Color fillColor) {
            Symbolizer symbolizer = null;
            Fill fill = null;
            Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));
    
            switch (geometryType) {
                case POLYGON:
                    fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));
                    symbolizer = sf.createPolygonSymbolizer(stroke, fill, geometryAttributeName);
                    break;
    
                case LINE:
                    symbolizer = sf.createLineSymbolizer(stroke, geometryAttributeName);
                    break;
    
                case POINT:
                    fill = sf.createFill(ff.literal(fillColor), ff.literal(OPACITY));
    
                    Mark mark = sf.getCircleMark();
                    mark.setFill(fill);
                    mark.setStroke(stroke);
    
                    Graphic graphic = sf.createDefaultGraphic();
                    graphic.graphicalSymbols().clear();
                    graphic.graphicalSymbols().add(mark);
                    graphic.setSize(ff.literal(POINT_SIZE));
    
                    symbolizer = sf.createPointSymbolizer(graphic, geometryAttributeName);
            }
    
            Rule rule = sf.createRule();
            rule.symbolizers().add(symbolizer);
            return rule;
        }
    
        private void setGeometry() {
            GeometryDescriptor geomDesc = featureSource.getSchema().getGeometryDescriptor();
            geometryAttributeName = geomDesc.getLocalName();
    
            Class<?> clazz = geomDesc.getType().getBinding();
    
            if (Polygon.class.isAssignableFrom(clazz) ||
                    MultiPolygon.class.isAssignableFrom(clazz)) {
                geometryType = GeomType.POLYGON;
    
            } else if (LineString.class.isAssignableFrom(clazz) ||
                    MultiLineString.class.isAssignableFrom(clazz)) {
    
                geometryType = GeomType.LINE;
    
            } else {
                geometryType = GeomType.POINT;
            }
    
        }
    
    	public void createPartControl(Composite parent) {
    						try {
    										
    					displayShapefile(parent);
    				} catch (Exception e) {
    					
    					e.printStackTrace();
    				}
    		
    		
    	}
    
    	public void setFocus() {
    	}
    }
    La classe addcordinnate()

    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    import com.sun.org.apache.bcel.internal.generic.NEW;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.LineString;
    import com.vividsolutions.jts.geom.MultiLineString;
    import java.awt.Color;
    import java.awt.SystemColor;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.persistence.EntityManager;
    
    import org.geotools.data.DataStore;
    import org.geotools.data.DefaultTransaction;
    import org.geotools.data.FeatureSource;
    import org.geotools.data.FeatureStore;
    import org.geotools.data.shapefile.ShapefileDataStore;
    import org.geotools.factory.CommonFactoryFinder;
    import org.geotools.factory.GeoTools;
    import org.geotools.feature.AttributeType;
    import org.geotools.feature.DefaultFeatureType;
    import org.geotools.feature.Feature;
    import org.geotools.feature.FeatureCollection;
    import org.geotools.feature.FeatureCollections;
    import org.geotools.feature.FeatureIterator;
    import org.geotools.feature.simple.SimpleFeatureBuilder;
    import org.geotools.feature.type.DefaultFeatureTypeBuilder;
    import org.geotools.map.DefaultMapLayer;
    import org.geotools.map.MapLayer;
    import org.geotools.styling.LineSymbolizer;
    import org.geotools.styling.Style;
    import org.geotools.styling.StyleBuilder;
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.simple.SimpleFeatureType;
    import org.opengis.feature.type.AttributeDescriptor;
    import org.opengis.feature.type.FeatureType;
    import org.opengis.feature.type.GeometryDescriptor;
    
    import org.opengis.feature.type.PropertyDescriptor;
    import org.opengis.filter.Filter;
    import org.opengis.filter.FilterFactory;
    import org.opengis.geometry.primitive.Point;
    
    import sofrecom.model.MaquetteEntityManagerFactory;
    import sofrecom.model.RXLCellule;
    import sofrecom.service.Rxl_Cellule_Service;
    import views.table.View_RXL_CELLULE_LEV_FULL_Table;
    
    
    
    public class MapUtil {
    	
    	public static  Integer fichierNMFId;
    
    	private Float[] longitude;
    	private Float[] latitude;
    	public static Integer getFichierNMFId() {
    			return fichierNMFId;
    		}
    
    		public static void setFichierNMFId(Integer fichierNMFId) {
    			View_RXL_CELLULE_LEV_FULL_Table.fichierNMFId = fichierNMFId;			
    		}
      
    		public MapUtil(Float[] longi, Float[] lati) {
            URL url = null;
           
            longitude=new Float[longi.length];
            latitude=new Float[lati.length];   
            longitude =longi;
            latitude=lati;
            try {
    			url =new File("C:/countries.shp").toURI().toURL();
    		} catch (MalformedURLException e) {
    			
    			e.printStackTrace();
    		}
    
            if (url != null) {
                MapLayer layer = createLayer(url);
    
                try {
            
                    ajouter(layer);
            
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        public Float[] getLongitude() {
    			return longitude;
    		}
    
    		public void setLongitude(Float[] longitude) {
    			this.longitude = longitude;
    		}
    
    		public Float[] getLatitude() {
    			return latitude;
    		}
    
    		public void setLatitude(Float[] latitude) {
    			this.latitude = latitude;
    		}
    
    	private MapLayer createLayer(URL url) {
    
            MapLayer layer = null;
            FeatureSource source = null;
    
            try {
                DataStore store = new ShapefileDataStore(url);
                String name = store.getTypeNames()[0];
                source = store.getFeatureSource(name);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            if (source != null) {
                Style style = createStyle();
                layer = new DefaultMapLayer(source, style);
            }
    
            return layer;
        }
    
        private Style createStyle() {
            StyleBuilder builder = new StyleBuilder();
    
            LineSymbolizer lineSymbol = builder.createLineSymbolizer(new Color(253, 241, 187), 2);
            Style lineStyle = builder.createStyle();
            lineStyle.addFeatureTypeStyle(builder.createFeatureTypeStyle(lineSymbol));
            return lineStyle;
        }
    
       
    
        private void ajouter(MapLayer layer) {
        	
        	
            FeatureStore store;     
            GeometryFactory geoFactory = new GeometryFactory();
            /******************************************************************/
            Coordinate[] coord2=new Coordinate[longitude.length];
            for(int i=0;i<coord2.length;i++){
            coord2[i]=new Coordinate(longitude[i], latitude[i]);}
            /******************************************************************/
    		LineString line = geoFactory.createLineString(coord2);
            MultiLineString lines = geoFactory.createMultiLineString(new LineString[]{line});
            SimpleFeatureType featureType = (SimpleFeatureType) layer.getFeatureSource().getSchema();
            Object[] values = new Object[featureType.getAttributeCount()];
            AttributeDescriptor geomAttribut = featureType.getGeometryDescriptor();
            List<AttributeDescriptor> attributes = featureType.getAttributeDescriptors();
    
            for (int i = 0,  max = attributes.size(); i < max; i++) {
                AttributeDescriptor oneAttribut = attributes.get(i);
    
                if (oneAttribut.equals(geomAttribut)) {
                    values[i] = lines;
                } else {
                    values[i] = oneAttribut.getDefaultValue();
                }
            }
    
            SimpleFeature myFeature = SimpleFeatureBuilder.build(featureType, values, null);
            FeatureCollection lstFeatures = FeatureCollections.newCollection();
            lstFeatures.add(myFeature);
    
            if (layer.getFeatureSource() instanceof FeatureStore) {
                store = (FeatureStore) layer.getFeatureSource();
    
                DefaultTransaction transaction = new DefaultTransaction();
                store.setTransaction(transaction);
                try {
                    store.addFeatures(lstFeatures);
                    System.out.println("*************"+ store.getBounds());
                    transaction.commit();                
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        store.getTransaction().rollback();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }finally{
                    transaction.close();
                }
    
            }
        
        }
     public static void main(String[]args){
    	 Float[] lati=new Float[50];
    	 Float[] longi=new Float[50];
    	 for(int i=0;i<50;i++)
    	 {
    		 lati[i]=20.5441654f +i;
    		 longi[i]=20.5441654f +i;
    	 }
    	new MapUtil(longi,lati);
     }
    
      
    }
    Bonne chance.
    Et si quelqu'un a une astuce pour changer les couleurs des Coordonnées je lui serai reconnaissant.

Discussions similaires

  1. Manipuler les fichiers Matlab en binaire
    Par melleb dans le forum MATLAB
    Réponses: 4
    Dernier message: 09/07/2008, 12h51
  2. Geotools et les fichiers Rasters
    Par pham1980 dans le forum SIG : Système d'information Géographique
    Réponses: 2
    Dernier message: 27/06/2007, 13h20
  3. Geotools et les fichiers Rasters
    Par pham1980 dans le forum 2D
    Réponses: 2
    Dernier message: 27/06/2007, 13h20
  4. Les commandes SQL*Plus manipulant les fichiers
    Par Maria1505 dans le forum Sql*Plus
    Réponses: 3
    Dernier message: 08/11/2006, 12h30
  5. Réponses: 8
    Dernier message: 26/08/2006, 02h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo