GeoJSON-Tools is a JavaScript module for working with location data, primarily using the GeoJSON specification.
### getDistance(array, decimals [optional])
Calculates the distance of an array of locations.
Arguments
- array - an array of locations, in the format
[lat, lng]
. - decimals - the decimal points to round answer to, defaults to 3 decimal points.
Example
var array = [
[20, 30],
[20.5, 29.5]
];
getDistance(array, 4);
To get the distance between two points, pass two points in the array, to get the distance of a linestring, pass the coordinates of the linestring.
### complexify(linestring, distance)
Convert LineString
or array of coordinates to a 'complex' line with specified maximum distance between each set of points.
__Argumenta""
- linestring - a valid GeoJSON
LineString
, or an array of locations, in the format [lat, lng]
. - distance - the maximum distance between each two locations, in meters.
Example
var array = [
[20, 30],
[20.5, 29.5]
];
complexify(array, .5);
distance of 'simple' linestring: 76.321 km
distance of 'complex' linesting: 76.321 km
Note: The algorithms works relatively well, and will return results that are > 99.9% accurate over short distances. Specifying distances > 500 km might return undesireable results.
We have also not added the option to specify precision, and precision is set at the default rounding of 3 decimals, this is to avoid infinite loops.
Conversions
### toGeoJSON(array, type)
Takes an input of an array and a GeoJSON
type, and returns that GeoJSON
object.
Arguments
- array - an array of locations, in the format
[lat, lng]
. - type - the type of
GeoJSON
object to return (note that this is not case-sensitive.
The default type is 'Point', which is returned when pushing an array of a single set of coordinates.
Other types are LineString
and Polygon
.
Note: Other GeoJSON
types will be supported in future versions.
Examples
To convert to GeoJSON::Point
Note that the array can be shallow or nested, but has to contain only one set of coordinates.
var array = [[20, 30]];
toGeoJSON(array);
To convert to GeoJSON::LineString
var array = [
[20, 30],
[20.5, 29.5]
];
toGeoJSON(array, 'linestring');
To convert to GeoJSON::Polygon
The minimum number of coordinates should be 4, and there is no need to add the last coordinate in the array.
Polygons
with holes are supported, however we currently do not test the validity of the holes.
var array = [
[20, 30],
[20.5, 29.5],
[21, 30.5]
];
toGeoJSON(array, 'polygon');
### toArray(geoobj)
Takes an input of a GeoJSON
type, and returns coordinates in [lat, lng]
format.
A single set of coordinates for a Point
, or an array for other types.
Arguments
- geoobj - a valid
GeoJSON
object of the following types: Point
, LineString
or Polygon
.
Note: Other GeoJSON
types will be supported in future versions.
Examples
To convert from GeoJSON::Point
Note that the array can be shallow or nested, but has to contain only one set of coordinates.
var geoobj = {"type":"Point","coordinates":[30,20]};
toArray(geoobj);
To convert from GeoJSON::LineString
var geoobj = {"type":"LineString","coordinates":[[30,20],[29.5,20.5]]};
toArray(geoobj);
To convert from GeoJSON::Polygon
The minimum number of coordinates should be 3, and there is no need to add the last coordinate in the array.
var geoobj = {"type":"Polygon","coordinates":[[[30,20],[29.5,20.5],[30.5,21],[30,20]]]};
toArray(geoobj);