6 package com.google.appinventor.components.runtime;
8 import android.text.TextUtils;
9 import android.util.Log;
11 import androidx.annotation.VisibleForTesting;
36 import java.util.ArrayList;
37 import java.util.LinkedList;
38 import java.util.List;
40 import org.json.JSONArray;
41 import org.json.JSONException;
42 import org.locationtech.jts.geom.Geometry;
43 import org.osmdroid.util.GeoPoint;
51 @DesignerComponent(version = YaVersion.POLYGON_COMPONENT_VERSION,
52 category = ComponentCategory.MAPS,
53 description =
"Polygon")
56 private static final String TAG =
Polygon.class.getSimpleName();
58 private List<List<GeoPoint>> points =
new ArrayList<List<GeoPoint>>();
59 private List<List<List<GeoPoint>>> holePoints =
new ArrayList<List<List<GeoPoint>>>();
60 private boolean multipolygon =
false;
61 private boolean initialized =
false;
65 public Double visit(
MapMarker marker, Object... arguments) {
66 if ((Boolean) arguments[1]) {
74 public Double visit(
MapLineString lineString, Object... arguments) {
75 if ((Boolean) arguments[1]) {
83 public Double visit(
MapPolygon polygon, Object... arguments) {
84 if ((Boolean) arguments[1]) {
92 public Double visit(
MapCircle circle, Object... arguments) {
93 if ((Boolean) arguments[1]) {
101 public Double visit(
MapRectangle rectangle, Object... arguments) {
102 if ((Boolean) arguments[1]) {
125 description =
"Returns the type of the feature. For polygons, this returns the text "
133 description =
"Gets or sets the sequence of points used to draw the polygon.")
135 if (points.isEmpty()) {
137 }
else if (multipolygon) {
139 List<YailList> result =
new LinkedList<YailList>();
140 for (List<GeoPoint> part : points) {
161 multipolygon =
false;
169 "Unable to determine the structure of the points argument.");
186 @SuppressWarnings(
"squid:S00100")
188 @
SimpleProperty(description =
"Constructs a polygon from the given list of coordinates.")
190 if (TextUtils.isEmpty(pointString)) {
191 points =
new ArrayList<List<GeoPoint>>();
196 JSONArray content =
new JSONArray(pointString);
197 if (content.length() == 0) {
198 points =
new ArrayList<List<GeoPoint>>();
199 multipolygon =
false;
204 multipolygon = points.size() > 1;
209 }
catch(JSONException e) {
220 description =
"Gets or sets the sequence of points used to draw holes in the polygon.")
222 if (holePoints.isEmpty()) {
224 }
else if (multipolygon) {
225 List<YailList> result =
new LinkedList<YailList>();
226 for (List<List<GeoPoint>> polyholes : holePoints) {
245 if (points.
size() == 0) {
246 holePoints =
new ArrayList<List<List<GeoPoint>>>();
247 }
else if (multipolygon) {
250 List<List<List<GeoPoint>>> holes =
new ArrayList<List<List<GeoPoint>>>();
252 this.holePoints = holes;
255 "Unable to determine the structure of the points argument.");
274 @SuppressWarnings(
"squid:S00100")
276 @
SimpleProperty(description =
"Constructs holes in a polygon from a given list of coordinates per hole.")
278 if (TextUtils.isEmpty(pointString)) {
279 holePoints =
new ArrayList<List<List<GeoPoint>>>();
284 JSONArray content =
new JSONArray(pointString);
285 if (content.length() == 0) {
286 holePoints =
new ArrayList<List<List<GeoPoint>>>();
295 Log.d(TAG,
"Points: " + points);
296 }
catch(JSONException e) {
297 Log.e(TAG,
"Unable to parse point string", e);
307 @
SimpleFunction(description =
"Returns the centroid of the Polygon as a (latitude, longitude) pair.")
309 return super.Centroid();
324 return visitor.
visit(
this, arguments);
335 this.points.addAll(points);
341 this.holePoints.clear();
342 this.holePoints.addAll(points);
347 boolean isInitialized() {