Search code examples
javageotools

How do you move an envelope to a specific point?


Point p = /*a point*/;
ReferencedEnvelope envelope = mapPane.getDisplayArea();
/*envelope.moveTo or something like that? */
mapPane.setDisplayArea(envelope);

How can I move the envelope so that the center position is the position of the given point? All I see are "expand"-methods.


Solution

  • The easiest way is to create a new envelope centred on your new point:

    ReferencedEnvelope env = fr.getMapPane().getDisplayArea();
    double x,y;//new centre
    double w2 = env.getWidth()/2.0;
    double h2 = env.getHeight()/2.0;
    Coordinate c = new Coordinate(x,y);
    ReferencedEnvelope renv = new ReferencedEnvelope(c.x-w2,c.x+w2,c.y-h2,c.y+h2,env.getCoordinateReferenceSystem());