Probleme avec un code d'une application sig avec l'api geotools

zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011 - 15 mai 2010 à 01:13
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011 - 21 mai 2010 à 01:31
Bonjour, voila je dois réaliser une application sig avec geotools. Ainsi au cours de mes recherches, j'ai trouvé un code implémentant geotools et qui aboutit à la réalisation d'une mini-application sig. Toutefois, il y a des erreurs( notamment à propos du casting) dans ce code et je les ai corrigé. Noter que je travaille sur éclipse et j'ai télécharger les librairies geotools nécessaires : d'ailleurs aucune erreurs de package ne s'affiche.
Seulement voila que cela ne marche pas quand je l'exécute alors que dans le programme il y a pas une seule erreurs signalée. Donc aidez moi svp !

voici le code :

//***************************** D E B U T *******************************

package application.java;

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.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
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.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
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.ComplexAttribute;
import org.opengis.feature.Property;
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;



public class Chap2Donnees{

public Chap2Donnees() {
URL url = null;
try {
url = new File("c:/shape/occ_sol.shp").toURI().toURL();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}

if (url != null) {
MapLayer layer = createLayer(url);

try {
explorer(layer);
parcourir(layer);
ajouter(layer);
supprimer(layer);
modifier(layer);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

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 explorer(MapLayer layer) throws IOException {

FeatureType type = layer.getFeatureSource().getSchema();

GeometryDescriptor geodesc = (GeometryDescriptor) ((SimpleFeature) type).getDefaultGeometry();
System.out.println("GEOM : " + geodesc.getName());
System.out.println("GEOM : " + geodesc.getType().getBinding());


Collection properties = ((ComplexAttribute) type).getProperties();
Iterator propertiesIte = properties.iterator();

while (propertiesIte.hasNext()) {
PropertyDescriptor oneProperty = (PropertyDescriptor) propertiesIte.next();
System.out.println(oneProperty.getName() + " : " + oneProperty.getType().getBinding());
}
}

private void parcourir(MapLayer layer) throws IOException {

FeatureCollection features = layer.getFeatureSource().getFeatures();
FeatureIterator featuresIte = features.features();

while (featuresIte.hasNext()) {
SimpleFeature oneFeature = (SimpleFeature) featuresIte.next();

for (int i 0, max oneFeature.getAttributeCount(); i < max; i++) {
System.out.println(oneFeature.getAttribute(i));
}
}
}

private void ajouter(MapLayer layer) {
FeatureStore store;

//(1)
GeometryFactory geoFactory = new GeometryFactory();

//(2)
Coordinate coord1 = new Coordinate(50, 430);
Coordinate coord2 = new Coordinate(21, 712);
LineString line = geoFactory.createLineString(new Coordinate[]{coord1, coord2});
MultiLineString lines = geoFactory.createMultiLineString(new LineString[]{line});

SimpleFeatureType featureType = (SimpleFeatureType) layer.getFeatureSource().getSchema();

//(3)
Object[] values = new Object[featureType.getAttributeCount()];

AttributeDescriptor geomAttribut = (AttributeDescriptor) ((SimpleFeature) featureType).getDefaultGeometry();
List attributes = featureType.getAttributeDescriptors();

//(4)
for (int i 0, max attributes.size(); i < max; i++) {
AttributeDescriptor oneAttribut = attributes.get(i);

//(5)
if (oneAttribut.equals(geomAttribut)) {
values[i] = lines;
} else {
values[i] = oneAttribut.getDefaultValue();
}
}

//(6)
SimpleFeature myFeature = SimpleFeatureBuilder.build(featureType, values, null);

//(7)
FeatureCollection lstFeatures = FeatureCollections.newCollection();
lstFeatures.add(myFeature);

//(8)
if (layer.getFeatureSource() instanceof FeatureStore) {
store = (FeatureStore) layer.getFeatureSource();

DefaultTransaction transaction = new DefaultTransaction();
store.setTransaction(transaction);

//(9)
try {
store.addFeatures(lstFeatures);
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
try {
store.getTransaction().rollback();
} catch (IOException e) {
e.printStackTrace();
}
}finally{
transaction.close();
}

}

}

private void supprimer(MapLayer layer) throws IOException {
FeatureStore store;

//(1)
FeatureCollection features = layer.getFeatureSource().getFeatures();
SimpleFeature oneFeature = (SimpleFeature) features.features().next();


//(2)
if (layer.getFeatureSource() instanceof FeatureStore) {

store = (FeatureStore) layer.getFeatureSource();
//(3)
DefaultTransaction transaction = new DefaultTransaction("trans_maj");
store.setTransaction(transaction);

//(4)
FilterFactory factory = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
Filter filter = factory.id(Collections.singleton(factory.featureId(oneFeature.getID())));

//(5)
try {
store.removeFeatures(filter);
transaction.commit();
} catch (IOException ex) {
ex.printStackTrace();
try {
transaction.rollback();
} catch (IOException e) {
e.printStackTrace();
}
}finally{
transaction.close();
}
}
}

private void modifier(MapLayer layer) throws IOException {
FeatureStore store;

//(1)
FeatureCollection features = layer.getFeatureSource().getFeatures();
SimpleFeature oneFeature = (SimpleFeature) features.features().next();

//(2)
AttributeDescriptor oneAttribut = (AttributeDescriptor) ((SimpleFeature) layer.getFeatureSource().getSchema()).getAttribute("NB_VOIES");

//(3)
String value = "4";


//(4)
if (layer.getFeatureSource() instanceof FeatureStore) {

store = (FeatureStore) layer.getFeatureSource();
//(5)
DefaultTransaction transaction = new DefaultTransaction("trans_maj");
store.setTransaction(transaction);

//(6)
FilterFactory factory = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
Filter filter = factory.id(Collections.singleton(factory.featureId(oneFeature.getID())));

//(7)
try {
store.modifyFeatures(oneAttribut, value, filter);
transaction.commit();
} catch (IOException ex) {
ex.printStackTrace();
try {
transaction.rollback();
} catch (IOException e) {
e.printStackTrace();
}
}finally{
transaction.close();
}
}
}

public static void main(String[] args) {
new Chap2Donnees();
}
}

//******************************* F I N **********************************


Et voici l'erreur à l'exécution :

//******************************* D E B U T ******************************

15 mai 2010 00:03:58 org.geotools.factory.FactoryRegistry scanForPlugins
ATTENTION: Échec lors de l'initialisation d'un service de catégorie "MathTransformProvider". La cause est "NoClassDefFoundError: javax/media/jai/WarpAffine".
Exception in thread "main" java.lang.NoSuchFieldError: LONG_OF_CENTRE
at org.geotools.referencing.operation.projection.ObliqueMercator$Provider_Hotine.<clinit>(ObliqueMercator.java:726)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.misc.Service$LazyIterator.next(Unknown Source)
at org.geotools.factory.FactoryRegistry.register(FactoryRegistry.java:829)
at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:773)
at org.geotools.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:808)
at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:195)
at org.geotools.referencing.operation.DefaultMathTransformFactory.getProvider(DefaultMathTransformFactory.java:281)
at org.geotools.referencing.operation.DefaultMathTransformFactory.getDefaultParameters(DefaultMathTransformFactory.java:315)
at org.geotools.referencing.wkt.Parser.parseProjection(Parser.java:584)
at org.geotools.referencing.wkt.Parser.parseProjCS(Parser.java:888)
at org.geotools.referencing.wkt.Parser.parseCoordinateReferenceSystem(Parser.java:222)
at org.geotools.referencing.wkt.Parser.parseCoordinateReferenceSystem(Parser.java:201)
at org.geotools.referencing.factory.ReferencingObjectFactory.createFromWKT(ReferencingObjectFactory.java:1088)
at org.geotools.data.shapefile.prj.PrjFileReader.(PrjFileReader.java:78)
at org.geotools.data.shapefile.ShapefileDataStore.openPrjReader(ShapefileDataStore.java:504)
at org.geotools.data.shapefile.ShapefileDataStore.readAttributes(ShapefileDataStore.java:658)
at org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:610)
at org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:604)
at org.geotools.data.shapefile.ShapefileDataStore.getFeatureSource(ShapefileDataStore.java:959)
at application.java.Chap2Donnees.createLayer(Chap2Donnees.java:78)
at application.java.Chap2Donnees.(Chap2Donnees.java:56)
at application.java.Chap2Donnees.main(Chap2Donnees.java:278)

//******************************** F I N **********************************

Merci d'avance !

15 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
15 mai 2010 à 07:33
Salut,

Un NoClassDefFound veut bien dire ce que ca veux dire : il ne trouve pas une classe. Il doit te manquer un jar dans le classpath de ton projet.
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
15 mai 2010 à 19:13
salut
pourtant j'ai ajouter les librairies(tous les jar qui s'y trouvent) : geotools 2.5.5 et geotools 2.6.0 .
Donc quel jar manquant ca pourrait être ? svp aidez moi !
Merci d'avance !
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
15 mai 2010 à 19:24
Salut,

La classe en question fait partie de la bibliothèque jai (java advanced imaging).
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
16 mai 2010 à 13:02
salut,
vous pouvez m'indiquez ou télécharger et comment installer cette bibliothèque jai; car j'ai téléchargé le .exe et le .zip de cette jai puis je les ai, respectivement, installé et ajouter(dans eclipse) : mais ca marche toujours pas!

Merci !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
16 mai 2010 à 13:04
Salut,

ajouter(dans eclipse)


Tu les as ajouté où ? Il faut que la bibliothèque soit ajoutée dans le classpath de ton application, sans cà, tu auras toujours cette erreur.
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
16 mai 2010 à 13:55
voila, je les ai ajouter : j'ai ajouter d'abord le .zip dans le classpath ensuite j'ai installe le .jdk . Maintenant la nouvelle erreurs est :

//********************** D E B U T *******************************
java.lang.NullPointerException: Null URL for ShapefileDataSource
at org.geotools.data.shapefile.ShapefileDataStore.(ShapefileDataStore.java:135)
at application.sig.Chap1Contact.buildMap(Chap1Contact.java:99)
at application.sig.Chap1Contact.(Chap1Contact.java:61)
at application.sig.Chap1Contact.main(Chap1Contact.java:176)

//************************ F I N **********************************

Merci !
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
16 mai 2010 à 14:39
Salut,

Tu n'as pas dû bien initialiser la classe ShapeFileDataStore lors de l'appel du constructeur (ligne 99 de ta classe Chap1Contact).
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
16 mai 2010 à 15:41
voila le bout du code qui concerne l'erreur (je pense) :

//******************* D E B U T *******************************
...
URL shapeURL Chap1Contact.class.getResource("/ressource/occ_sol.shp"); ShapefileDataStore store new ShapefileDataStore(shapeURL);
String name store.getTypeNames()[0]; FeatureSource source store.getFeatureSource(name);
...

//************************** F I N ***********************************

donc comment regler le probleme ?

Merci encore pour votre temps !
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
16 mai 2010 à 15:57
Salut,

Regarde ce que te retourne Chap1Contact.class.getResource("/ressource/occ_sol.shp") mais apparemment il ne trouve pas le fichier (il doit te renvoyer une URL null).
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
16 mai 2010 à 16:17
pourtant le fichier est bien présent dans le répertoire ressource lequel j'ai bel et bien placé dans mon package(en effet j'ai créer le répertoire ressource dans mon package et ensuite j'ai copié puis collé le fichier occ_sol.shp la dedans).
Alors que devrais je faire d'autre? est ce que la procedure n'est-elle pas correct? ou bien je dois placer le dossier quelque part sur le disque et indiquer le chemin? je sais vraiment plus !

N.B : j'ai importé plutôt la librairie geotools 2.3.2

Merci encore!
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
16 mai 2010 à 16:47
Salut,

Il faut que ton répertoire soit relatif à la racine de ton projet, et non à la racine de ta classe.

Par exemple, si ta classe est dans :
/src/package/Classe.class

Et que ton fichier est dans :
/ressources/images/image.jpg

Alors ca doit être :
Classe.class.getResource("/ressource/images/image.jpg");
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
16 mai 2010 à 21:50
oui,c est ce que j'ai fais le probleme persiste : toujours l'erreur

java.lang.NullPointerException: Null URL for ShapefileDataSource
at org.geotools.data.shapefile.ShapefileDataStore.(ShapefileDataStore.java:135)
at application.sig.Chap1Contact.buildMap(Chap1Contact.java:99)
at application.sig.Chap1Contact.(Chap1Contact.java:61)
at application.sig.Chap1Contact.main(Chap1Contact.java:176)
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
19 mai 2010 à 01:03
salut,
alors vous ne dites rien; ne me laisser pas tomber svp, j'ai besoin de votre aide !
merci !
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
19 mai 2010 à 08:42
Salut,

C'est que je n'ai plus vraiment d'idée, je ne connais pas la bibliothèque que tu utilises.
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
zizona Messages postés 63 Date d'inscription mercredi 4 novembre 2009 Statut Membre Dernière intervention 22 décembre 2011
21 mai 2010 à 01:31
bon ce n'est pas grave, je vais encore fouillée sur le net. Si je comprends pas un truck je reviendrai alors.

Merci pour tout !
0
Rejoignez-nous