AI2 Component  (Version nb184)
LineString.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright © 2016-2020 Massachusetts Institute of Technology, All rights reserved.
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
10 
11 import android.util.Log;
12 import androidx.annotation.NonNull;
32 import java.util.ArrayList;
33 import java.util.List;
34 import org.json.JSONArray;
35 import org.json.JSONException;
36 import org.locationtech.jts.geom.Geometry;
37 import org.osmdroid.util.GeoPoint;
38 
45 @DesignerComponent(version = YaVersion.LINESTRING_COMPONENT_VERSION,
46  category = ComponentCategory.MAPS,
47  description = "LineString")
48 @SimpleObject
49 public class LineString extends MapFeatureBase implements MapLineString {
50  private static final String TAG = LineString.class.getSimpleName();
51  private List<GeoPoint> points = new ArrayList<GeoPoint>();
52 
53  private static final MapFeatureVisitor<Double> distanceComputation = new MapFeatureVisitor<Double>() {
54  @Override
55  public Double visit(MapMarker marker, Object... arguments) {
56  if ((Boolean) arguments[1]) {
57  return GeometryUtil.distanceBetweenCentroids(marker, (LineString) arguments[0]);
58  } else {
59  return GeometryUtil.distanceBetweenEdges(marker, (LineString) arguments[0]);
60  }
61  }
62 
63  @Override
64  public Double visit(MapLineString lineString, Object... arguments) {
65  if ((Boolean) arguments[1]) {
66  return GeometryUtil.distanceBetweenCentroids(lineString, (LineString) arguments[0]);
67  } else {
68  return GeometryUtil.distanceBetweenEdges(lineString, (LineString) arguments[0]);
69  }
70  }
71 
72  @Override
73  public Double visit(MapPolygon polygon, Object... arguments) {
74  if ((Boolean) arguments[1]) {
75  return GeometryUtil.distanceBetweenCentroids((LineString) arguments[0], polygon);
76  } else {
77  return GeometryUtil.distanceBetweenEdges((LineString) arguments[0], polygon);
78  }
79  }
80 
81  @Override
82  public Double visit(MapCircle circle, Object... arguments) {
83  if ((Boolean) arguments[1]) {
84  return GeometryUtil.distanceBetweenCentroids((LineString) arguments[0], circle);
85  } else {
86  return GeometryUtil.distanceBetweenEdges((LineString) arguments[0], circle);
87  }
88  }
89 
90  @Override
91  public Double visit(MapRectangle rectangle, Object... arguments) {
92  if ((Boolean) arguments[1]) {
93  return GeometryUtil.distanceBetweenCentroids((LineString) arguments[0], rectangle);
94  } else {
95  return GeometryUtil.distanceBetweenEdges((LineString) arguments[0], rectangle);
96  }
97  }
98  };
99 
101  super(container, distanceComputation);
102  StrokeWidth(3);
103  container.addFeature(this);
104  }
105 
107  description = "Returns the type of the map feature. For LineString, this returns "
108  + "the text \"LineString\".")
109  @Override
110  public String Type() {
111  return MapFactory.MapFeatureType.TYPE_LINESTRING;
112  }
113 
115  description = "A list of latitude and longitude pairs that represent the line segments " +
116  "of the polyline.")
117  @Override
118  public YailList Points() {
119  return GeometryUtil.pointsListToYailList(points);
120  }
121 
127  @Override
128  public void Points(@NonNull YailList points) {
129  if (points.size() < 2) {
130  container.$form().dispatchErrorOccurredEvent(this, "Points",
131  ErrorMessages.ERROR_LINESTRING_TOO_FEW_POINTS, points.length() - 1);
132  } else {
133  try {
134  this.points = GeometryUtil.pointsFromYailList(points);
135  clearGeometry();
137  } catch (DispatchableError e) {
138  container.$form().dispatchErrorOccurredEvent(this, "Points",
139  e.getErrorCode(), e.getArguments());
140  }
141  }
142  }
143 
152  public void PointsFromString(String points) {
153  final String functionName = "PointsFromString";
154  try {
155  List<GeoPoint> geopoints = new ArrayList<GeoPoint>();
156  JSONArray array = new JSONArray(points);
157  if (array.length() < 2) {
158  // Need at least two points
160  }
161  int length = array.length();
162  for (int i = 0; i < length; ++i) {
163  JSONArray point = array.optJSONArray(i);
164  if (point == null) {
166  array.get(i).toString());
167  } else if (point.length() < 2) {
169  points.length());
170  }
171  double latitude = point.optDouble(0, Double.NaN);
172  double longitude = point.optDouble(1, Double.NaN);
173  if (!isValidLatitude(latitude)) {
175  i, array.get(0).toString());
176  } else if (!isValidLongitude(longitude)) {
178  i, array.get(1).toString());
179  }
180  geopoints.add(new GeoPoint(latitude, longitude));
181  }
182  this.points = geopoints;
183  clearGeometry();
185  } catch(JSONException e) {
186  Log.e(TAG, "Malformed string to LineString.PointsFromString", e);
188  } catch(DispatchableError e) {
189  container.$form().dispatchErrorOccurredEvent(this, functionName, e.getErrorCode(), e.getArguments());
190  }
191  }
192 
197  @Override
198  @DesignerProperty(defaultValue = "3")
200  public void StrokeWidth(int width) {
201  super.StrokeWidth(width);
202  }
203 
204  @Override
205  public List<GeoPoint> getPoints() {
206  return points;
207  }
208 
209  @Override
210  public <T> T accept(MapFeatureVisitor<T> visitor, Object... arguments) {
211  return visitor.visit(this, arguments);
212  }
213 
214  @Override
215  protected Geometry computeGeometry() {
216  return GeometryUtil.createGeometry(points);
217  }
218 
219  @Override
220  public void updatePoints(List<GeoPoint> points) {
221  this.points = points;
222  clearGeometry();
223  }
224 }
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.util.MapFactory.MapCircle
Definition: MapFactory.java:1028
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.addFeature
void addFeature(MapFeature feature)
com.google.appinventor.components.runtime.errors.DispatchableError
Definition: DispatchableError.java:12
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_EXPECTED_ARRAY_AT_INDEX
static final int ERROR_EXPECTED_ARRAY_AT_INDEX
Definition: ErrorMessages.java:249
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.MapFactory
Definition: MapFactory.java:30
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_LONGITUDE_IN_POINT_AT_INDEX
static final int ERROR_INVALID_LONGITUDE_IN_POINT_AT_INDEX
Definition: ErrorMessages.java:248
com.google.appinventor.components.runtime.util.MapFactory.MapController.updateFeaturePosition
void updateFeaturePosition(MapMarker marker)
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.LineString.Type
String Type()
Definition: LineString.java:110
com.google.appinventor.components
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_LATITUDE_IN_POINT_AT_INDEX
static final int ERROR_INVALID_LATITUDE_IN_POINT_AT_INDEX
Definition: ErrorMessages.java:247
com.google.appinventor.components.runtime.util.MapFactory.MapLineString
Definition: MapFactory.java:1373
com.google.appinventor.components.runtime.util.MapFactory.MapPolygon
Definition: MapFactory.java:1410
com.google.appinventor.components.runtime.LineString.PointsFromString
void PointsFromString(String points)
Definition: LineString.java:152
com.google.appinventor.components.runtime.util.GeometryUtil.pointsListToYailList
static YailList pointsListToYailList(List<? extends IGeoPoint > points)
Definition: GeometryUtil.java:84
com.google.appinventor.components.runtime.util.GeometryUtil.pointsFromYailList
static List< GeoPoint > pointsFromYailList(YailList points)
Definition: GeometryUtil.java:104
com.google.appinventor.components.runtime.Map.getController
MapController getController()
Definition: Map.java:667
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenEdges
static double distanceBetweenEdges(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:249
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.util.MapFactory.MapRectangle
Definition: MapFactory.java:1099
com.google.appinventor.components.runtime.MapFeatureBase
Definition: MapFeatureBase.java:32
com.google.appinventor.components.runtime.LineString.updatePoints
void updatePoints(List< GeoPoint > points)
Definition: LineString.java:220
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenCentroids
static double distanceBetweenCentroids(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:327
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor.visit
T visit(MapMarker marker, Object... arguments)
com.google.appinventor.components.runtime.MapFeatureBase.container
MapFeatureContainer container
Definition: MapFeatureBase.java:33
com.google.appinventor.components.runtime.LineString.computeGeometry
Geometry computeGeometry()
Definition: LineString.java:215
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_LINESTRING_TOO_FEW_FIELDS
static final int ERROR_LINESTRING_TOO_FEW_FIELDS
Definition: ErrorMessages.java:232
com.google.appinventor.components.runtime.MapFeatureBase.map
Map map
Definition: MapFeatureBase.java:34
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA
static final String PROPERTY_TYPE_TEXTAREA
Definition: PropertyTypeConstants.java:248
com.google.appinventor.components.runtime.util.MapFactory.MapMarker
Definition: MapFactory.java:1205
com.google.appinventor.components.runtime.util.GeometryUtil.isValidLongitude
static boolean isValidLongitude(double longitude)
Definition: GeometryUtil.java:454
com.google.appinventor.components.runtime.errors.DispatchableError.getArguments
Object[] getArguments()
Definition: DispatchableError.java:34
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_LINESTRING_PARSE_ERROR
static final int ERROR_LINESTRING_PARSE_ERROR
Definition: ErrorMessages.java:231
com.google.appinventor.components.runtime.LineString.Points
YailList Points()
Definition: LineString.java:118
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.GeometryUtil
Definition: GeometryUtil.java:41
com.google.appinventor.components.runtime.LineString.LineString
LineString(MapFactory.MapFeatureContainer container)
Definition: LineString.java:100
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.util.GeometryUtil.createGeometry
static Geometry createGeometry(GeoPoint point)
Definition: GeometryUtil.java:120
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.MapFeatureBase.StrokeWidth
int StrokeWidth()
Definition: MapFeatureBase.java:203
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureType
Definition: MapFactory.java:1483
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_LINESTRING_TOO_FEW_POINTS
static final int ERROR_LINESTRING_TOO_FEW_POINTS
Definition: ErrorMessages.java:230
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.MapFeatureBase.clearGeometry
final synchronized void clearGeometry()
Definition: MapFeatureBase.java:430
com.google.appinventor.components.runtime.LineString.StrokeWidth
void StrokeWidth(int width)
Definition: LineString.java:200
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.LineString.Points
void Points(@NonNull YailList points)
Definition: LineString.java:128
com.google.appinventor.components.runtime.LineString.getPoints
List< GeoPoint > getPoints()
Definition: LineString.java:205
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.LineString
Definition: LineString.java:49
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer
Definition: MapFactory.java:800
com.google.appinventor.components.runtime.util.GeometryUtil.isValidLatitude
static boolean isValidLatitude(double latitude)
Definition: GeometryUtil.java:444
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor
Definition: MapFactory.java:745
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.errors.DispatchableError.getErrorCode
int getErrorCode()
Definition: DispatchableError.java:30