Hi All,

 

I am working on a project where I have nautical charts displayed within a
MapContent object within a JMapFrame.  Then I plot points onto that map,
each set of points within their own layer.  I want to give the user the
ability to select individual points and groups of points either for deletion
or other purposes.  In order to accomplish this, I create a listener for
mouse clicks (code below).  When I click on any points that are on the map,
it appears as though the only feature I get is the Grid that is the raster
map itself.  I don't ever find any of the points which are displaying.  Does
anyone have an idea as to what I may be doing wrong here?  Also, has anyone
had any experience with selecting many points by clicking/dragging to cover
many?  I appreciate your time!

 

      // Add a listener for mouse clicks on the map to select points

      mapFrame.getMapPane().addMouseListener( new MapMouseAdapter() {

         @Override

         public void onMouseClicked(MapMouseEvent ev) {

            // Get the world position of the mouse

            //DirectPosition2D pos = ev.getMapPosition();

            DirectPosition2D pos = ev.getWorldPos();

            java.awt.Point screenPos = ev.getPoint();

            // Not collecting WayPoints, select features based on click and
highlight them

            Rectangle screenRect = new Rectangle(screenPos.x-1,
screenPos.y-1, 3, 3);

            /*

             * Transform the screen rectangle into bounding box in the
coordinate

             * reference system of our map context. Note: we are using a
naive method

             * here but GeoTools also offers other, more accurate methods.

             */

            AffineTransform screenToWorld =
mapFrame.getMapPane().getScreenToWorldTransform();

            Rectangle2D worldRect =
screenToWorld.createTransformedShape(screenRect).getBounds2D();

            ReferencedEnvelope worldbbox = new ReferencedEnvelope(worldRect,
mapFrame.getMapContent().getCoordinateReferenceSystem());

 

            mapFeatureSource = (SimpleFeatureSource)
rasterLayer.getFeatureSource();

            

            // transform from world to target CRS

            SimpleFeatureType schema = mapFeatureSource.getSchema();

            CoordinateReferenceSystem targetCRS =
schema.getCoordinateReferenceSystem();

            String geometryAttributeName =
schema.getGeometryDescriptor().getLocalName();

                     

            try {

               ReferencedEnvelope bbox = worldbbox.transform(targetCRS,
true, 10);

               /*

                * Create a Filter to select features that intersect with

                * the bounding box

                */

               Filter filter = ff.bbox(ff.property(geometryAttributeName),
bbox);

               //Filter filter =
ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));

                      

               /*

                * Use the filter to identify the selected features

                */

                     

               SimpleFeatureCollection selectedFeatures =
mapFeatureSource.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().getID() + "; Type: " + feature.getFeatureType());

                  }

               } finally {

                  iter.close();

               }

               if (IDs.isEmpty()) {

                  System.out.println("   no feature selected");

               }

            } catch (Exception ex) {

               ex.printStackTrace();

               return;

            }

            System.out.println("Mouse Click latitude:  " + pos.y);

            System.out.println("Mouse Click longitude:  " + pos.x);

         }

      } );

 

 

Thanks,

David

 

--------------------------------------------

David Purdy

Owner/President

JJM Technologies Corporation

Email:  david.pu...@jjmtechcorp.com

Phone:  508.972.1266

signatureLogo

 

<<image001.gif>>

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to