Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.commons.geojson;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mapbox.services.commons.geojson.custom.GeometryDeserializer;
import com.mapbox.services.commons.geojson.custom.PositionDeserializer;
Expand All @@ -16,7 +17,7 @@ public class Feature implements GeoJSON {

private final String type = "Feature";
private final Geometry geometry;
private final JsonObject properties;
private JsonObject properties;
private final String id;

/**
Expand Down Expand Up @@ -60,6 +61,10 @@ public JsonObject getProperties() {
return properties;
}

public void setProperties(JsonObject properties) {
this.properties = properties;
}

/**
* The optional, common identifier of this feature.
*
Expand Down Expand Up @@ -123,4 +128,112 @@ public String toJson() {
gson.registerTypeAdapter(Position.class, new PositionSerializer());
return gson.create().toJson(this);
}

/**
* Convenience method to add a String member.
* @param key name of the member
* @param value the String value associated with the member
*/
public void addStringProperty(String key, String value) {
this.properties.addProperty(key, value);
}

/**
* Convenience method to add a Number member.
* @param key name of the member
* @param value the Number value associated with the member
*/
public void addNumberProperty(String key, Number value) {
this.properties.addProperty(key, value);
}

/**
* Convenience method to add a Boolean member.
* @param key name of the member
* @param value the Boolean value associated with the member
*/
public void addBooleanProperty(String key, Boolean value) {
this.properties.addProperty(key, value);
}

/**
* Convenience method to add a Character member.
* @param key name of the member
* @param value the Character value associated with the member
*/
public void addCharacterProperty(String key, Character value) {
this.properties.addProperty(key, value);
}

/**
* Convenience method to add a JsonElement member.
* @param key name of the member
* @param value the JsonElement value associated with the member
*/
public void addProperty(String key, JsonElement value) {
this.properties.add(key, value);
}

/**
* Convenience method to get a String member.
* @param key name of the member
* @return the value of the member, null if it doesn't exist
*/
public String getStringProperty(String key) {
return this.properties.get(key).getAsString();
}

/**
* Convenience method to get a Number member.
* @param key name of the member
* @return the value of the member, null if it doesn't exist
*/
public Number getNumberProperty(String key) {
return this.properties.get(key).getAsNumber();
}

/**
* Convenience method to get a Boolean member.
* @param key name of the member
* @return the value of the member, null if it doesn't exist
*/
public Boolean getBooleanProperty(String key) {
return this.properties.get(key).getAsBoolean();
}

/**
* Convenience method to get a Character member.
* @param key name of the member
* @return the value of the member, null if it doesn't exist
*/
public Character getCharacterProperty(String key) {
return this.properties.get(key).getAsCharacter();
}

/**
* Convenience method to get a JsonElement member.
* @param key name of the member
* @return the value of the member, null if it doesn't exist
*/
public JsonElement getProperty(String key) {
return this.properties.get(key);
}

/**
* Removes the property from the object properties
* @param key name of the member
* @return
*/
public JsonElement removeProperty(String key) {
return this.properties.remove(key);
}

/**
* Convenience method to check if a member with the specified name is present in this object.
* @param key
* @return
*/
public boolean hasProperty(String key) {
return this.properties.has(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* Interface implemented by all Geometry objects, contains common fields.
* @param <T> the type of the coordinates, normally a list interface of positions.
*/
public interface Geometry<T> extends com.mapbox.services.commons.geojson.GeoJSON {
public interface Geometry<T> extends GeoJSON {

T getCoordinates();
void setCoordinates(T coordinates);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class LineString implements Geometry<List<Position>> {

private final String type = "LineString";
private final List<Position> coordinates;
private List<Position> coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -47,6 +47,11 @@ public List<Position> getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(List<Position> coordinates) {
this.coordinates = coordinates;
}

/**
* creates a {@link LineString} from a list of coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class MultiLineString implements Geometry<List<List<Position>>> {

private final String type = "MultiLineString";
private final List<List<Position>> coordinates;
private List<List<Position>> coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -46,6 +46,11 @@ public List<List<Position>> getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(List<List<Position>> coordinates) {
this.coordinates = coordinates;
}

/**
* Creates a {@link MultiLineString} from a list of coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class MultiPoint implements Geometry<List<Position>> {

private final String type = "MultiPoint";
private final List<Position> coordinates;
private List<Position> coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -46,6 +46,11 @@ public List<Position> getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(List<Position> coordinates) {
this.coordinates = coordinates;
}

/**
* Creates a {@link MultiPoint} from a list of coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class MultiPolygon implements Geometry<List<List<List<Position>>>> {

private final String type = "MultiPolygon";
private final List<List<List<Position>>> coordinates;
private List<List<List<Position>>> coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -46,6 +46,11 @@ public List<List<List<Position>>> getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(List<List<List<Position>>> coordinates) {
this.coordinates = coordinates;
}

/**
* Creates a {@link MultiPolygon} from a list of coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class Point implements Geometry<Position> {

private final String type = "Point";
private final Position coordinates;
private Position coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -44,6 +44,11 @@ public Position getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(Position coordinates) {
this.coordinates = coordinates;
}

/**
* Creates a {@link Point} from a given coordinate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class Polygon implements Geometry<List<List<Position>>> {

private final String type = "Polygon";
private final List<List<Position>> coordinates;
private List<List<Position>> coordinates;

/**
* Private constructor.
Expand Down Expand Up @@ -46,6 +46,11 @@ public List<List<Position>> getCoordinates() {
return coordinates;
}

@Override
public void setCoordinates(List<List<Position>> coordinates) {
this.coordinates = coordinates;
}

/**
* Creates a {@link Polygon} from a list of coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mapbox.services.commons.turf;

/**
* Created by antonio on 5/12/16.
*/
public class TurfConstants {

public final static String UNIT_MILES = "miles";
public final static String UNIT_NAUTICAL_MILES = "nauticalmiles";
public final static String UNIT_KILOMETERS = "kilometers";
public final static String UNIT_RADIANS = "radians";
public final static String UNIT_DEGREES = "degrees";
public final static String UNIT_INCHES = "inches";
public final static String UNIT_YARDS = "yards";
public final static String UNIT_METERS = "meters";

public final static String UNIT_DEFAULT = UNIT_KILOMETERS;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mapbox.services.commons.turf;

/**
* Created by antonio on 5/12/16.
*/
public class TurfException extends Exception {

public TurfException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.mapbox.services.commons.turf;

import java.util.HashMap;
import java.util.Map;

/**
* Created by antonio on 5/12/16.
*/
public class TurfHelpers {

private static final Map<String, Double> factors;
static
{
factors = new HashMap<>();
factors.put(TurfConstants.UNIT_MILES, 3960d);
factors.put(TurfConstants.UNIT_NAUTICAL_MILES, 3441.145d);
factors.put(TurfConstants.UNIT_DEGREES, 57.2957795d);
factors.put(TurfConstants.UNIT_RADIANS, 1d);
factors.put(TurfConstants.UNIT_INCHES, 250905600d);
factors.put(TurfConstants.UNIT_YARDS, 6969600d);
factors.put(TurfConstants.UNIT_METERS, 637300d);
factors.put(TurfConstants.UNIT_KILOMETERS, 6373d);

// Also supported
factors.put("metres", 637300d);
factors.put("kilometres", 6373d);
}

public static double radiansToDistance(double radians) throws TurfException {
return radiansToDistance(radians, TurfConstants.UNIT_DEFAULT);
}

public static double radiansToDistance(double radians, String units) throws TurfException {
Double factor = factors.get(units);
if (factor == null) {
throw new TurfException("Invalid unit.");
}

return radians * factor;
}

public static double distanceToRadians(double radians) throws TurfException {
return distanceToRadians(radians, TurfConstants.UNIT_DEFAULT);
}

public static double distanceToRadians(double radians, String units) throws TurfException {
Double factor = factors.get(units);
if (factor == null) {
throw new TurfException("Invalid unit.");
}

return radians / factor;
}

}
Loading