Select to view content in your preferred language

Transform a Point into a Polygon (Square)

726
2
02-16-2021 07:25 AM
GuilhermeFernandes
Emerging Contributor

Hello. I am having trouble  to implementing a new functionality in my application.

Based on a point (latitude and longitude),  I want that this point automatically transform into a square, with this lat and long as the center. I'm trying something like this to transform the point:

double ang__ = 0;
double co_x1 = (sin(toRadians(ang__ + 90)) * widthImplement) + point.getX();
double co_y1 = (cos(toRadians(ang__ + 90)) * widthImplement) + point.getY();
double co_x2 = (sin(toRadians(ang__ + 270)) * widthImplement) + point.getX();
double co_y2 = (cos(toRadians(ang__ + 270)) * widthImplement) + point.getY();

But it's not working. 

Has anyone already done something like that?
Note: I would like to present width options for this square on the screen.

0 Kudos
2 Replies
MarkBaird
Esri Regular Contributor

You will find it much easier if you convert your point which you want as the centre of your square into a projected coordinate system.  Once you've done that you can offset in metres from the location and things are much easier to understand.

 

              SimpleLineSymbol simpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF00FF00, 3); 
              // a point in a field near London, UK
              wgs84Point = new Point(0,51, SpatialReferences.getWgs84());

              // convert wgs84 (lat / long) into projected coordinate system
              Point wmPoint = (Point) GeometryEngine.project(wgs84Point, SpatialReferences.getWebMercator());

              // size of your square in metres
              double size = 100;

              // easier logic for point of square with offsets in metres
              Point p1 = new Point(wmPoint.getX() + (size / 2), wmPoint.getY() + (size /2), SpatialReferences.getWebMercator());
              Point p2 = new Point(wmPoint.getX() + (size / 2), wmPoint.getY() - (size /2), SpatialReferences.getWebMercator());
              Point p3 = new Point(wmPoint.getX() - (size / 2), wmPoint.getY() - (size /2), SpatialReferences.getWebMercator());
              Point p4 = new Point(wmPoint.getX() - (size / 2), wmPoint.getY() + (size /2), SpatialReferences.getWebMercator());

              // make polygon from points
              PointCollection pointCollection = new PointCollection(SpatialReferences.getWebMercator());
              pointCollection.add(p1);
              pointCollection.add(p2);
              pointCollection.add(p3);
              pointCollection.add(p4);

              Polygon square = new Polygon(pointCollection);

              // Graphic and add to graphics overlay to display it
              Graphic squareGraphic = new Graphic(square, simpleLineSymbol);
              graphicsOverlay.getGraphics().add(squareGraphic);

 

Not a pretty bit of code, but it should point you in the right direction.

Does this help

0 Kudos
GuilhermeFernandes
Emerging Contributor

Thank you so much Mark. Worked perfectly.

0 Kudos