0

Motive:

Im creating an android Application that does the following.

  1. Reads input points (of type Point - a custom class).
  2. Calculates the outermost points.
  3. Draws a polygon (needs input of type GeoPoint from osmdroid library) with those points as the vertices (a convex polygon).

Problem:

I made the algorithm to work. But there is a problem converting the datatypes.

The input is a Point array(Point is a custom class).

geoPoints = new Point[7];
geoPoints[0] = new Point(new GeoPoint(8.180992, 77.336551));

But the osmdroid function that draws the polygon needs List < GeoPoint > as input.

List<GeoPoint> gPoints;
polygon.setPoints(gPoints);

I managed to convert this on the Point() constructor like something below

public Point(GeoPoint geoPoint) {
        this.x = geoPoint.getLatitude();
        this.y = geoPoint.getLongitude();
    }

But I have no idea how work out the return type.

SO, when I run the code. I get the following error.

error: incompatible types: inference variable T has incompatible bounds
equality constraints: GeoPoint
lower bounds: Point
where T is a type-variable:
T extends Object declared in method <T>asList(T...)

Calling Function:

private void callingFunction() {

        geoPoints = new Point[7];
        geoPoints[0] = new Point(new GeoPoint(8.180992, 77.336551));
        geoPoints[1] = new Point(new GeoPoint(8.183966, 77.340353));
        geoPoints[2] = new Point(new GeoPoint(8.179836, 77.336105));
        geoPoints[3] = new Point(new GeoPoint(8.178744, 77.339179));
        geoPoints[4] = new Point(new GeoPoint(8.182155, 77.341925));
        geoPoints[5] = new Point(new GeoPoint(8.181655, 77.339318));
        geoPoints[6] = new Point(new GeoPoint(8.182155, 77.341925));

        polygon = new Polygon();    //see note below
        polygon.setFillColor(Color.parseColor("#80FFE082"));
        polygon.setStrokeColor(Color.parseColor("#FFD54F"));
        polygon.setStrokeWidth(5f);

        ConvexHull cx = new ConvexHull(geoPoints);
        Point C1[]= cx.getConvexHull();

        polygon.setPoints(Arrays.asList(C1));

        map.getOverlayManager().add(polygon);
        map.invalidate();

    }

Note:

Using only List or only Array is not possible as

polygon.setPoints(geoPoints)    //needs List<GeoPoint> in callingFunction
Arrays.sort(points);            //requires array of Points in ConvexHull

Everything provided. How can I solve this problem? Any sorts of help will be really useful. And Thanks in advance.

2
  • Can you change your Point class to extend GeoPoint? Then you could just pass the List. Commented Aug 24, 2018 at 8:38
  • 1
    Not really - sorry! Did you read the page about how to create an MCVE? It's not so much about just reducing the amount of code you post but rather about creating the smallest possible example you can - probably in a whole new project - that boils it down to the specific problem and nothing else. It's also a good idea to read How do I ask a good question? - note in particular "Pretend you're talking to a busy colleague" - nobody has time to read all that much code when they're offering their help for free. Commented Aug 24, 2018 at 8:59

1 Answer 1

1

I'm not sure, if I got you right, but basically your problem boils down to the fact, that you have an array of Point objects, but need a list of GeoPoint objects instead, right? And there is no relation between Point and GeoPoint, so one does not extend the other.

So why not translate Point objects back to GeoPoint objects before drawing the polygon?

You could map types in a stream:

ConvexHull cx = new ConvexHull(geoPoints);
Point C1[] = cx.getConvexHull();

polygon.setPoints(Arrays.stream(C1).map(p -> new GeoPoint(p.getX(), p.getY())).collect(Collectors.toList());

Or use a for loop:

ConvexHull cx = new ConvexHull(geoPoints);
Point C1[] = cx.getConvexHull();

List<GeoPoint> points = new ArrayList<GeoPoint>(C1.length);
for (int i = 0; i < C1.length; ++i) {
  points.add(new GeoPoint(C1[i].getX(), C1[i].getY()));
}
polygon.setPoints(points);
Sign up to request clarification or add additional context in comments.

4 Comments

The call requires API 24. My curent min is 16. I cant change the min from 16 as we are targetting devices running older versions of android. Is there any workaround ?
Use a for loop to transform the Point array to a list of GeoPoint objects.
I've edited my answer and added a for loop alternative.
Thanks for your super fast reply. Your code actually solved my issue. Have been working this for 2 day now. And im marking this as the accepted answer. Thanks a lot. Off topic but, is there any way there is a desktop app for S.O. im getting the notifications a little late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.