Content-Type: multipart/alternative; boundary="----=_NextPart_001_0123_01CEA98A.DB7BFCC0" ------=_NextPart_001_0123_01CEA98A.DB7BFCC0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 IDs = new HashSet(); 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.purdy@jjmtechcorp.com Phone: 508.972.1266 signatureLogo ------=_NextPart_001_0123_01CEA98A.DB7BFCC0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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

  &= nbsp;   mapFrame.getMapPane()= .addMouseListener( new<= /b> = MapMouseAdapter() {

  &= nbsp;      @Override

  &= nbsp;      public = void= = onMouseClicked(MapMouseEvent ev) {

  &= nbsp;         // Get the = world position of the mouse

  &= nbsp;         //DirectPos= ition2D pos =3D ev.getMapPosition();

  &= nbsp;         DirectPosition2D = pos =3D ev.getWorldPos();

  &= nbsp;         java.awt.Point = screenPos =3D ev.getPoint();

  &= nbsp;         // Not = collecting WayPoints, select features based on click and highlight = them

  &= nbsp;         Rectangle screenRect = =3D new<= /b> = Rectangle(screenPos.x-1, = screenPos.y-1, 3, = 3);

  &= nbsp;         /*

  = ;           * 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.

  = ;           */

  &= nbsp;         AffineTransform = screenToWorld =3D mapFrame.getMapPane()= .getScreenToWorldTransform();

  &= nbsp;         Rectangle2D worldRect = =3D = screenToWorld.createTransformedShape(screenRect).getBounds2D();

  &= nbsp;         ReferencedEnvelope = worldbbox =3D new<= /b> = ReferencedEnvelope(worldRect, mapFrame.getMapConten= t().getCoordinateReferenceSystem());

 <= /p>

    &= nbsp;       mapFeature= Source =3D (SimpleFeatureSource) = rasterLayer.getFeatureSource();

  &= nbsp;         

  &= nbsp;         // = transform from world to target CRS

  &= nbsp;         SimpleFeatureType = schema =3D mapFeatureS= ource.getSchema();=

  &= nbsp;         = CoordinateReferenceSystem targetCRS =3D = schema.getCoordinateReferenceSystem();

  &= nbsp;         String = geometryAttributeName =3D = schema.getGeometryDescriptor().getLocalName();

  &= nbsp;    =             &= nbsp;

  &= nbsp;         try<= /b> = {

  &= nbsp;            = ReferencedEnvelope bbox =3D worldbbox.transform(targetCRS, = true= , = 10);

  &= nbsp;            = /*

  = ;     =          * Create a Filter to = select features that intersect with

  = ;             =  * the bounding box

  = ;     =          */

  &= nbsp;            = Filter filter =3D ff.bbox(= ff.property(geo= metryAttributeName), bbox);

  &= nbsp;            = //Filter = filter =3D ff.intersects(ff.property(geometryAttributeName), = ff.literal(bbox));

  &= nbsp;    =             &= nbsp; 

  &= nbsp;    =         /*

  = ;             =  * Use the filter to identify the selected features

  = ;     =          */

  &= nbsp;    =             &= nbsp;

  &= nbsp;    =         SimpleFeatureCollection = selectedFeatures =3D mapFeatureS= ource.getFeatures(= filter);

  &= nbsp;   

  &= nbsp;            = SimpleFeatureIterator iter =3D selectedFeatures.features();

  &= nbsp;           =  Set<FeatureId> IDs =3D new<= /b> = HashSet<FeatureId>();

  &= nbsp;            = try<= /b> = {

  &= nbsp;    =            = while = (iter.hasNext()) {

  &= nbsp;    =             &= nbsp; SimpleFeature feature =3D iter.next();

  &= nbsp;    =              =  IDs.add(feature.getIdentifier());

  &= nbsp;    =             &= nbsp; System.out<= /i>.println(" = ;  " + = feature.getIdentifier().getID() + "; = Type: " + = feature.getFeatureType());

  &= nbsp;    =            = }

  &= nbsp;            } = finally = {

  &= nbsp;    =           =  iter.close();

  &= nbsp;            = }

  &= nbsp;            = if = (IDs.isEmpty()) {

  &= nbsp;    =            = System.out<= /i>.println(" = ;  no feature selected");

  &= nbsp;           =  }

  &= nbsp;         } catch = (Exception ex) {

  &= nbsp;            = ex.printStackTrace();

  &= nbsp;           =  return;

  &= nbsp;         }

  &= nbsp;         = System.out<= /i>.println("Mouse= Click latitude:  " + = pos.y);

  &= nbsp;         = System.out<= /i>.println("Mouse= Click longitude:  " + = pos.x);

  &= nbsp;      }

  &= nbsp;   } );

 

 

Thanks,

David

 

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

David Purdy

Owner/President

JJM Technologies Corporation

Email:  = david.purdy@jjmtechcorp.com

Phone:  508.972.1266

3DsignatureLogo

 

------=_NextPart_001_0123_01CEA98A.DB7BFCC0--