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

Java Discussion :

Problème avec Geotools et ShapeFile


Sujet :

Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2009
    Messages : 42
    Points : 37
    Points
    37
    Par défaut Problème avec Geotools et ShapeFile
    Salut à tous

    Je travail sur un projet Tuteuré de cartographie en Java avec la librairie Geotools.

    Je cherche pour ma part à ouvrir une carte au format shp. Que cette carte s'ouvre à l'exécution du programme.
    J'ai importé les librairie Geotools 2.3.2 ainsi que JAI

    Voila le code:
    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
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import org.geotools.data.FeatureSource;
    import org.geotools.data.Query;
    import org.geotools.data.shapefile.ShapefileDataStore;
    import org.geotools.gui.swing.JMapPane;
    import org.geotools.map.DefaultMapContext;
    import org.geotools.map.DefaultMapLayer;
    import org.geotools.map.MapContext;
    import org.geotools.map.MapLayer;
    import org.geotools.renderer.lite.StreamingRenderer;
    import org.geotools.styling.PolygonSymbolizer;
    import org.geotools.styling.Style;
    import org.geotools.styling.StyleBuilder;
     
    public class Quickstart {
     
        private JMapPane mappane = new JMapPane();
     
        public Quickstart() {
            JFrame frm = new JFrame();
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setSize(800, 600);
            frm.setTitle("Ma Première carte avec GeoTools");
     
     
            JPanel pfond = new JPanel(new BorderLayout());
            frm.setContentPane(pfond);
     
            frm.setJMenuBar(buildMenu());
            frm.getContentPane().add(BorderLayout.CENTER, buildMap());
            frm.getContentPane().add(BorderLayout.NORTH, buildTool());
     
            frm.setVisible(true);
        }
     
        private JMenuBar buildMenu() {
            JMenuBar menu = new JMenuBar();
            JMenu mfichier = new JMenu("Fichier");
            JMenuItem iquitter = new JMenuItem("Quitter");
            iquitter.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            mfichier.add(iquitter);
            menu.add(mfichier);
     
            return menu;
        }
     
        private JPanel buildMap() {
     
            mappane.setBackground(new Color(157, 201, 255));
     
            try {
     
                MapContext mapcontext = new DefaultMapContext();
                mapcontext.setTitle("Projet");
     
                mappane.setContext(mapcontext);
     
                MapLayer maplayer;
                URL shapeURL = Quickstart.class.getResource("./france.shp");
     
                ShapefileDataStore store = new ShapefileDataStore(shapeURL);
                String name = store.getTypeNames()[0];
                FeatureSource source = store.getFeatureSource(name);
                StyleBuilder sb = new StyleBuilder();
     
                PolygonSymbolizer ps = sb.createPolygonSymbolizer(new Color(253, 241, 187), new Color(163, 151, 97), 1);
                Style solstyle = sb.createStyle();
                solstyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(ps));
                maplayer = new DefaultMapLayer(source, solstyle);
                maplayer.setTitle("france.shp");
                maplayer.setVisible(true);
                maplayer.setQuery(Query.ALL);
                mapcontext.addLayer(maplayer);
     
     
     
                StreamingRenderer render = new StreamingRenderer();
     
                mappane.setRenderer(render);
                mappane.setMapArea(mapcontext.getLayerBounds());
     
     
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            return mappane;
        }
     
         private JPanel buildTool() {
            JPanel outil = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
            JButton plus = new JButton("+");
            plus.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.ZoomIn);
                }
            });
            outil.add(plus);
     
            JButton moins = new JButton("-");
            moins.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.ZoomOut);
                }
            });
            outil.add(moins);
     
            JButton pan = new JButton("Pan");
            pan.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.Pan);
                }
            });
            outil.add(pan);
     
            return outil;
        }
     
     
         public static void main(String[] args){
             new Quickstart();
         }
     
    }
    Je me trouve face à 2 problème.

    Le 1er n'a pas l'air de véritablement affecter le fonctionnement mais je ne sais pas quoi y faire, Eclipse me dit que cette méthode:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (MapContext mapcontext = new DefaultMapContext())
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    MapContext mapcontext = new DefaultMapContext();
                mapcontext.setTitle("Projet");
     
                mappane.setContext(mapcontext);
    est dépréciée.

    Ensuite lorsque j'exécute le programme la fenêtre s'ouvre avec un fond bleu (normal) mais aucune carte ne s'affiche.
    La compilation me dis que la variable URL est NULL
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    MapLayer maplayer;
                URL shapeURL = Quickstart.class.getResource("./france.shp");
     
                ShapefileDataStore store = new ShapefileDataStore(shapeURL);
    donc que ma variable est vide.
    J'ai copié le fichier shp a plusieurs endroit (avec le fichier source contenant le code et dans le dossier précédent)

    Quelqu'un aurait-il donc une solution pour que mon ShapeFile s'ouvre dans ma fenêtre ?

    Merci à tous

  2. #2
    Membre confirmé Avatar de ngpub
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    449
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 449
    Points : 505
    Points
    505
    Par défaut
    Dans MapContext aucune méthode setContext(...) n'est documentée ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Quickstart.class.getResource("france.shp");
    Dans ce cas le fichier "france.shp" doit être à côté du fichier "Quickstart.class" pour être trouvé.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2009
    Messages : 42
    Points : 37
    Points
    37
    Par défaut
    le fichier shape est avec mon fichier contenant le code mais je viens de remarquer un truc c'est que la méthode java.net.URL n'est pas trouvée
    La compilation dit ca:

    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at Quickstart.buildMap(Quickstart.java:80)
    at Quickstart.<init>(Quickstart.java:43)
    at Quickstart.main(Quickstart.java:147)

    alors que mes import y sont et ne donnent pas de warnigs

  4. #4
    Membre confirmé Avatar de ngpub
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    449
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 449
    Points : 505
    Points
    505
    Par défaut
    at java.net.URL.<init>(Unknown Source)
    Pas d'affolement, "Unknown Source" dit juste qu'il n'a pas le numéro de ligne dans le code .

    Dans une pile d'exception l'une des infos les plus intéressante est généralement la 1er ligne, si ça a à voir avec ton problème, que dit-elle ?

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2009
    Messages : 42
    Points : 37
    Points
    37
    Par défaut
    Dsl j'avais pas vu qu'il y avait d'autre trucs au dessus ^^

    Voila la pile complète

    java.net.MalformedURLException: no protocol: france.shp
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at Quickstart.buildMap(Quickstart.java:79)
    at Quickstart.<init>(Quickstart.java:42)
    at Quickstart.main(Quickstart.java:146)

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2009
    Messages : 42
    Points : 37
    Points
    37
    Par défaut
    J'ai aussi un peu modifier le code pour le stockage de l'URL

    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
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.*;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import org.geotools.data.FeatureSource;
    import org.geotools.data.Query;
    import org.geotools.data.shapefile.ShapefileDataStore;
    import org.geotools.gui.swing.JMapPane;
    import org.geotools.map.DefaultMapContext;
    import org.geotools.map.DefaultMapLayer;
    import org.geotools.map.MapContext;
    import org.geotools.map.MapLayer;
    import org.geotools.renderer.lite.StreamingRenderer;
    import org.geotools.styling.PolygonSymbolizer;
    import org.geotools.styling.Style;
    import org.geotools.styling.StyleBuilder;
     
    public class Quickstart {
     
        private JMapPane mappane = new JMapPane();
     
        public Quickstart() {
            JFrame frm = new JFrame();
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setSize(800, 600);
            frm.setTitle("Carte de France");
     
     
            JPanel pfond = new JPanel(new BorderLayout());
            frm.setContentPane(pfond);
     
            frm.setJMenuBar(buildMenu());
            frm.getContentPane().add(BorderLayout.CENTER, buildMap());
            frm.getContentPane().add(BorderLayout.NORTH, buildTool());
     
            frm.setVisible(true);
        }
     
        private JMenuBar buildMenu() {
            JMenuBar menu = new JMenuBar();
            JMenu mfichier = new JMenu("Fichier");
            JMenuItem iquitter = new JMenuItem("Quitter");
            iquitter.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            mfichier.add(iquitter);
            menu.add(mfichier);
     
            return menu;
        }
     
        private JPanel buildMap() {
     
            mappane.setBackground(new Color(157, 201, 255));
     
            try {
     
                MapContext mapcontext = new DefaultMapContext();
                mapcontext.setTitle("Projet");
     
                mappane.setContext(mapcontext);
     
     
     
                MapLayer maplayer;
                //URL shapeURL = Quickstart.class.getResource("./france.shp");
                URL shapeURL = new URL("france.shp");
     
     
                shapeURL.toString();
                System.out.print(shapeURL);
     
                ShapefileDataStore store = new ShapefileDataStore(shapeURL);
                String name = store.getTypeNames()[0];
                FeatureSource source = store.getFeatureSource(name);
                StyleBuilder sb = new StyleBuilder();
     
                PolygonSymbolizer ps = sb.createPolygonSymbolizer(new Color(253, 241, 187), new Color(163, 151, 97), 1);
                Style solstyle = sb.createStyle();
                solstyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(ps));
                maplayer = new DefaultMapLayer(source, solstyle);
                maplayer.setTitle("france.shp");
                maplayer.setVisible(true);
                maplayer.setQuery(Query.ALL);
                mapcontext.addLayer(maplayer);
     
     
     
                StreamingRenderer render = new StreamingRenderer();
     
                mappane.setRenderer(render);
                mappane.setMapArea(mapcontext.getLayerBounds());
     
     
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            return mappane;
        }
     
         private JPanel buildTool() {
            JPanel outil = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
            JButton plus = new JButton("+");
            plus.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.ZoomIn);
                }
            });
            outil.add(plus);
     
            JButton moins = new JButton("-");
            moins.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.ZoomOut);
                }
            });
            outil.add(moins);
     
            JButton pan = new JButton("Pan");
            pan.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    mappane.setState(JMapPane.Pan);
                }
            });
            outil.add(pan);
     
            return outil;
        }
     
     
         public static void main(String[] args){
             new Quickstart();
         }
     
    }

  7. #7
    Membre confirmé Avatar de ngpub
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    449
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 449
    Points : 505
    Points
    505
    Par défaut
    Pour avoir l'url d'un fichier, je ferais plus comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    URL shapeURL = new File("france.shp").toURI().toURL();
    Mais si ton shape est définitivement lié à ton appli, la façon de faire est de le placer dans un package ("mypackage.resources" par exemple) et d'obtenir sont URL avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    URL shapeURL = ClassLoader.getSystemResource("mypackage/resources/france.shp")

  8. #8
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2009
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2009
    Messages : 42
    Points : 37
    Points
    37
    Par défaut
    Ca marche avec ta première idée merci beaucoup

    Je testerais quand même la 2eme méthode

    Merci encore

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. VC++ Direct3D8, problème avec LPD3DXFONT et LPD3DTEXTURE8
    Par Magus (Dave) dans le forum DirectX
    Réponses: 3
    Dernier message: 03/08/2002, 11h10
  2. Problème avec le type 'Corba::Any_out'
    Par Steven dans le forum CORBA
    Réponses: 2
    Dernier message: 14/07/2002, 18h48
  3. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10

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