Thanks a lot. I try one different way, but It works...
//I use *miLista* to track the source of each shapefile
private List<FeatureSource<SimpleFeatureType, SimpleFeature>> miLista = new
ArrayList();
private void mostrarShapeFile(File mFile) throws Exception {
FileDataStore mDataStore = FileDataStoreFinder.getDataStore(mFile);
FeatureSource<SimpleFeatureType, SimpleFeature> _featuresource =
mDataStore.getFeatureSource();
establecerFigura(_featuresource);
Style mStyle = crearStyle();
mMapContext.addLayer(_featuresource, mStyle);
miLista.add(_featuresource);
}
//Function which select the geometry
private void seleccionarFigura(MapMouseEvent evt) {
System.out.println("Click en: " + evt.getMapPosition());
DirectPosition2D pos = evt.getMapPosition();
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
Geometry point = gf.createPoint(new Coordinate(pos.x, pos.y));
Filter mFilter = mFilterFactory.intersects(
mFilterFactory.property(mGeomName),
mFilterFactory.literal(point));
try {
//Read the layer ID to query from a JTextField, It's a test
case...
int capa_activa =
Integer.parseInt(mJTextField.getText()) > 0 ?
Integer.parseInt(mJTextField.getText()) : 0;
FeatureCollection<SimpleFeatureType, SimpleFeature>
mSelectedFeature;
//Select the shapefile to work...
FeatureSource<SimpleFeatureType, SimpleFeature> f =
miLista.get(capa_activa);
//Apply the filter
mSelectedFeature = f.getFeatures(mFilter);
FeatureIterator<SimpleFeature> mIterator =
mSelectedFeature.features();
Set<FeatureId> mId = new HashSet<FeatureId>();
try {
while (mIterator.hasNext()) {
SimpleFeature mFeature = mIterator.next();
mId.add(mFeature.getIdentifier());
}
} finally {
mIterator.close();
}
if (mId.isEmpty()) {
System.out.println("Ninguna figura fue
seleccionada."); //Nothing
was selected...
}
mostrarFiguraSeleccionada(mId, capa_activa); //Show the geometry
selected
} catch (Exception ex) {
ex.printStackTrace();
return;
}
}
private void mostrarFiguraSeleccionada(Set<FeatureId> mId, int capa) {
Style mStyle;
if (mId.isEmpty()) {
mStyle = crearStyle();
} else {
mStyle = crearStyleSeleccion(mId);
}
//clear all previous selections.
for (MapLayer l : mMapContext.getLayers()){
l.setStyle(crearStyle());
}
if (mMapContext.getLayerCount() == 0) {
return;
} else if (mMapContext.getLayerCount() == 1) {
mJmapFrame.getMapContext().getLayer(0).setStyle(mStyle); //If
there is only one layer then select It.
} else {
mMapContext.getLayer(capa).setSelected(true);
mJmapFrame.getMapContext().getLayer(capa).setStyle(mStyle); //Apply
the selection style to the selected geometry
}
mJmapFrame.getMapPane().repaint();
}
If somebody want to use this code or have a suggest, feel free to email me.
PD: Sorry for my spanglish (AGAIIIIIIIIIIN.) xD
------------------------------------------------------------------------------
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users