2

I'm using geotools v14.3 to load a shapefile (Java Swing application). I want to allow the customers to use the mouse to click on the map area to draw new points, lines and polygons. First, I'm testing the points. So, a customer should be able to draw a point on the clicked location.

My current approach

After following few articles, I first capture the clicked mouse location with a mapmouselistener.

@Override
    public void onMouseClicked(MapMouseEvent ev) {
        System.out.println("mouse clicked : " + ev.getWorldPos());
        DirectPosition2D p = ev.getWorldPos();
        drawMyPoint(p.getX(), p.getY());

    }

Point drawing function is as follows.

void drawMyPoint(double x, double y) {
        SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
        b.setName("MyFeatureType");

        // set crs
        b.setCRS(DefaultGeographicCRS.WGS84);
        // add geometry
        b.add("location", Point.class);

        // build the type
        final SimpleFeatureType TYPE = b.buildFeatureType();

        // create features using the type defined
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        com.vividsolutions.jts.geom.Point point = geometryFactory.createPoint(new Coordinate(x, y));
        featureBuilder.add(point);
        SimpleFeature feature = featureBuilder.buildFeature("FeaturePoint");
        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
        featureCollection.add(feature); //Add feature 1
        // Add feature 2, 3, etc

        Style style = SLD.createPointStyle("Star", Color.BLUE, Color.BLUE, 0.3f, 15);
        Layer layer = new FeatureLayer(featureCollection, style);
        layer.setTitle("NewPoint");
        map.addLayer(layer);
        mapPane.repaint();

    }

Problem

I can see the layer name being added to the map layer table. But the point is not visible on the map.

1

The most likely answer is axis order - if you check the DirectPosition2D API docs you will see the following warning:

This class inherits x and y fields. But despite their names, they don't need to be oriented toward East and North. The (x,y) axis can have any orientation and should be understood as "ordinate 0" and "ordinate 1" values instead. This is not specific to this implementation; in Java2D too, the visual axis orientation depend on the affine transform in the graphics context. The rational for avoiding axis orientation restriction is that other DirectPosition implementation do not have such restriction, and it would be hard to generalize (what to do with North-East direction?).

Yet when creating the Point later on to display on your map you have treated them as X & Y without checking. To be safe you should wrap that creation with some check code like:

        if (CRS.getAxisOrder(crs)
                .equals(AxisOrder.NORTH_EAST)) {
            points[i] = geomFactory.createPoint(new Coordinate(
                    x, y));
        } else {
            points[i] = geomFactory.createPoint(new Coordinate(
                    y, x));
        }
  • Thank yout @iant for the helpful answer. I just commented the crs setting code and it started working. // set crs b.setCRS(DefaultGeographicCRS.WGS84); To brief what I did, I load a shape file layer first, then add a mouse listener, as I wanted users to add new nodes on to the map. When I comment the above setCRS() for a new point, it succeeded. Any hint why this happens? And I will read and test what was proposed. – tybandara Apr 23 '16 at 9:16
  • Instead of assuming your map is in lat/lon you should probably do b.setCRS(map.getCoordinateReferenceSystem()); – Ian Turton Apr 23 '16 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.