Module: Turf
- Extended by:
- Turf
- Included in:
- Turf
- Defined in:
- lib/turf.rb,
lib/turf/tag.rb,
lib/turf/tin.rb,
lib/turf/area.rb,
lib/turf/bbox.rb,
lib/turf/flip.rb,
lib/turf/mask.rb,
lib/turf/meta.rb,
lib/turf/along.rb,
lib/turf/angle.rb,
lib/turf/clone.rb,
lib/turf/kinks.rb,
lib/turf/union.rb,
lib/turf/buffer.rb,
lib/turf/center.rb,
lib/turf/circle.rb,
lib/turf/convex.rb,
lib/turf/length.rb,
lib/turf/random.rb,
lib/turf/rewind.rb,
lib/turf/sample.rb,
lib/turf/sector.rb,
lib/turf/square.rb,
lib/turf/bearing.rb,
lib/turf/collect.rb,
lib/turf/combine.rb,
lib/turf/concave.rb,
lib/turf/ellipse.rb,
lib/turf/explode.rb,
lib/turf/flatten.rb,
lib/turf/helpers.rb,
lib/turf/version.rb,
lib/turf/voronoi.rb,
lib/turf/centroid.rb,
lib/turf/clusters.rb,
lib/turf/dissolve.rb,
lib/turf/distance.rb,
lib/turf/envelope.rb,
lib/turf/hex_grid.rb,
lib/turf/isobands.rb,
lib/turf/isolines.rb,
lib/turf/line_arc.rb,
lib/turf/midpoint.rb,
lib/turf/simplify.rb,
lib/turf/truncate.rb,
lib/turf/bbox_clip.rb,
lib/turf/intersect.rb,
lib/turf/invariant.rb,
lib/turf/tesselate.rb,
lib/turf/difference.rb,
lib/turf/line_chunk.rb,
lib/turf/line_slice.rb,
lib/turf/line_split.rb,
lib/turf/planepoint.rb,
lib/turf/point_grid.rb,
lib/turf/polygonize.rb,
lib/turf/projection.rb,
lib/turf/center_mean.rb,
lib/turf/destination.rb,
lib/turf/interpolate.rb,
lib/turf/line_offset.rb,
lib/turf/moran_index.rb,
lib/turf/square_grid.rb,
lib/turf/bbox_polygon.rb,
lib/turf/clean_coords.rb,
lib/turf/great_circle.rb,
lib/turf/lib/lineclip.rb,
lib/turf/line_overlap.rb,
lib/turf/line_segment.rb,
lib/turf/bezier_spline.rb,
lib/turf/boolean_equal.rb,
lib/turf/boolean_valid.rb,
lib/turf/center_median.rb,
lib/turf/geojson_rbush.rb,
lib/turf/nearest_point.rb,
lib/turf/rhumb_bearing.rb,
lib/turf/shortest_path.rb,
lib/turf/triangle_grid.rb,
lib/turf/boolean_within.rb,
lib/turf/center_of_mass.rb,
lib/turf/line_intersect.rb,
lib/turf/polygon_smooth.rb,
lib/turf/rectangle_grid.rb,
lib/turf/rhumb_distance.rb,
lib/turf/unkink_polygon.rb,
lib/turf/boolean_concave.rb,
lib/turf/boolean_crosses.rb,
lib/turf/boolean_overlap.rb,
lib/turf/boolean_touches.rb,
lib/turf/clusters_dbscan.rb,
lib/turf/clusters_kmeans.rb,
lib/turf/distance_weight.rb,
lib/turf/line_to_polygon.rb,
lib/turf/polygon_to_line.rb,
lib/turf/transform_scale.rb,
lib/turf/boolean_contains.rb,
lib/turf/boolean_disjoint.rb,
lib/turf/boolean_parallel.rb,
lib/turf/directional_mean.rb,
lib/turf/line_slice_along.rb,
lib/turf/point_on_feature.rb,
lib/turf/polygon_tangents.rb,
lib/turf/quadrat_analysis.rb,
lib/turf/transform_rotate.rb,
lib/turf/boolean_clockwise.rb,
lib/turf/rhumb_destination.rb,
lib/turf/boolean_intersects.rb,
lib/turf/transform_translate.rb,
lib/turf/boolean_point_on_line.rb,
lib/turf/nearest_point_on_line.rb,
lib/turf/nearest_point_to_line.rb,
lib/turf/points_within_polygon.rb,
lib/turf/point_to_line_distance.rb,
lib/turf/boolean_point_in_polygon.rb,
lib/turf/nearest_neighbor_analysis.rb,
lib/turf/point_to_polygon_distance.rb,
lib/turf/standard_deviational_ellipse.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Lineclip Classes: Error, NotImplementedError
Constant Summary collapse
- AREA_RADIUS =
6_378_137.0
- EARTH_RADIUS =
6_371_008.8
- FACTORS =
{ "centimeters" => EARTH_RADIUS * 100, "centimetres" => EARTH_RADIUS * 100, "degrees" => 360.0 / (2 * Math::PI), "feet" => EARTH_RADIUS * 3.28084, "inches" => EARTH_RADIUS * 39.37, "kilometers" => EARTH_RADIUS / 1000, "kilometres" => EARTH_RADIUS / 1000, "meters" => EARTH_RADIUS, "metres" => EARTH_RADIUS, "miles" => EARTH_RADIUS / 1609.344, "millimeters" => EARTH_RADIUS * 1000, "millimetres" => EARTH_RADIUS * 1000, "nauticalmiles" => EARTH_RADIUS / 1852, "radians" => 1.0, "yards" => EARTH_RADIUS * 1.0936 }.freeze
- AREA_FACTORS =
{ "acres" => 0.000247105, "centimeters" => 10_000.0, "centimetres" => 10_000.0, "feet" => 10.763910417, "hectares" => 0.0001, "inches" => 1550.003100006, "kilometers" => 0.000001, "kilometres" => 0.000001, "meters" => 1.0, "metres" => 1.0, "miles" => 3.86e-7, "nauticalmiles" => 2.9155334959812285e-7, "millimeters" => 1_000_000.0, "millimetres" => 1_000_000.0, "yards" => 1.195990046 }.freeze
- VERSION =
Version of turf-ruby
"1.1.0"
Measurement collapse
-
#along(line, distance, options = {}) ⇒ Feature<Point>
Takes a LineString and returns a Point at a specified distance along the line.
-
#bearing(from, to, options = {}) ⇒ number
Takes two points and finds the geographic bearing between them, i.e.
-
#centroid(geojson, properties = nil) ⇒ Feature<Point>
Takes one or more features and calculates the centroid using the mean of all vertices.
-
#destination(origin, distance, bearing, options = {}) ⇒ Feature<Point>
Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees.
-
#distance(from, to, options = {}) ⇒ number
Calculates the distance between two points in degrees, radians, miles, or kilometers.
-
#length(geojson, options = {}) ⇒ number
Takes a GeoJSON and measures its length in the specified units, (Multi)Point ‘s distance are ignored.
Helper collapse
-
#feature(geom, properties = nil, options = {}) ⇒ Feature
Wraps a GeoJSON Geometry in a GeoJSON Feature.
-
#feature_collection(features, options = {}) ⇒ FeatureCollection
Takes one or more Features and creates a FeatureCollection.
-
#geometry_collection(geometries, properties = nil, options = {}) ⇒ Feature<GeometryCollection>
Creates a Feature based on a coordinate array.
-
#line_string(coordinates, properties = nil, options = {}) ⇒ Feature<LineString>
Creates a LineString Feature from an Array of Positions.
-
#multi_line_string(coordinates, properties = nil, options = {}) ⇒ Feature<MultiLineString>
Creates a Feature<MultiLineString> based on a coordinate array.
-
#multi_point(coordinates, properties = nil, options = {}) ⇒ Feature<MultiPoint>
Creates a Feature based on a coordinate array.
-
#multi_polygon(coordinates, properties = nil, options = {}) ⇒ Feature<MultiPolygon>
Creates a Feature<MultiPolygon> based on a coordinate array.
-
#point(coordinates, properties = nil, options = {}) ⇒ Feature<Point>
Creates a Point Feature from a Position.
-
#polygon(coordinates, properties = nil, options = {}) ⇒ Feature<Polygon>
Creates a Polygon Feature from an Array of LinearRings.
Unit Conversion collapse
-
#azimuth_to_bearing(angle) ⇒ number
Converts any azimuth angle from the north line direction (positive clockwise) and returns an angle between -180 and +180 degrees (positive clockwise), 0 being the north line.
-
#bearing_to_azimuth(bearing) ⇒ number
Converts any bearing angle from the north line direction (positive clockwise) and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line.
-
#convert_area(area, original_unit = nil, final_unit = nil) ⇒ number
Converts an area from one unit to another.
-
#convert_length(length, original_unit = nil, final_unit = nil) ⇒ number
Converts a length from one unit to another.
-
#degrees_to_radians(degrees) ⇒ number
Converts an angle in degrees to radians.
-
#is_number(num) ⇒ boolean
Checks if the input is a number.
-
#is_object(input) ⇒ boolean
Checks if the input is an object.
-
#length_to_radians(distance, units = nil) ⇒ number
Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet.
-
#line_strings(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<LineString>
Creates a FeatureCollection from an Array of LineString coordinates.
-
#points(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<Point>
Creates a Point FeatureCollection from an Array of Point coordinates.
-
#polygons(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<Polygon>
Creates a Polygon FeatureCollection from an Array of Polygon coordinates.
-
#radians_to_degrees(radians) ⇒ number
Converts an angle in radians to degrees.
-
#radians_to_length(radians, units = nil) ⇒ number
Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
-
#round(num, precision = 0) ⇒ number
Rounds a number to a specified precision.
Meta collapse
-
#collection_of(feature_collection, type, name = nil) ⇒ Object
Enforce expectations about types of FeatureCollection inputs for Turf.
-
#contains_number(coordinates) ⇒ Boolean
Checks if coordinates contains a number.
-
#feature_of(feature = nil, type = nil, name = nil) ⇒ Object
Enforce expectations about types of Feature inputs for Turf.
-
#geojson_type(value = nil, type = nil, name = nil) ⇒ Object
Enforce expectations about types of GeoJSON objects for Turf.
-
#get_coord(coord) ⇒ Array
Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
-
#get_coords(coords) ⇒ Array
Unwrap coordinates from a Feature, Geometry Object or an Array.
-
#get_geom(geojson) ⇒ Geometry|null
Get Geometry from Feature or Geometry Object.
-
#get_type(geojson, _name = "geojson") ⇒ String
Get GeoJSON object’s type, Geometry type is prioritize.
Booleans collapse
-
#boolean_point_in_polygon(point, polygon, options = {}) ⇒ boolean
Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon.
Instance Method Summary collapse
- #angle(*args) ⇒ Object
- #apply_filter(*args) ⇒ Object
-
#area(geojson) ⇒ number
Takes one or more features and returns their area in square meters.
-
#bbox(geojson, options = {}) ⇒ Array<Float>
Calculates the bounding box for any GeoJSON object, including FeatureCollection.
-
#bbox_clip(feature, bbox) ⇒ Feature<LineString, MultiLineString, Polygon, MultiPolygon>
Takes a Feature and a bbox and clips the feature to the bbox using [lineclip](github.com/mapbox/lineclip).
-
#bbox_polygon(bbox, options = {}) ⇒ Feature<Polygon>
Takes a bbox and returns an equivalent polygon.
- #bezier_spline(*args) ⇒ Object
- #boolean_clockwise(line) ⇒ Object
-
#boolean_concave(polygon) ⇒ Boolean
Takes a polygon and returns true or false as to whether it is concave or not.
- #boolean_contains(*args) ⇒ Object
- #boolean_crosses(*args) ⇒ Object
- #boolean_disjoint(*args) ⇒ Object
- #boolean_equal(*args) ⇒ Object
- #boolean_intersects(*args) ⇒ Object
- #boolean_overlap(*args) ⇒ Object
- #boolean_parallel(*args) ⇒ Object
-
#boolean_point_on_line(point, line, options = nil) ⇒ Boolean
Returns true if a point is on a line.
- #boolean_touches(*args) ⇒ Object
- #boolean_valid(*args) ⇒ Object
- #boolean_within(*args) ⇒ Object
- #buffer(*args) ⇒ Object
-
#center(geojson, options = {}) ⇒ Feature<Point>
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
- #center_mean(*args) ⇒ Object
- #center_median(*args) ⇒ Object
- #center_of_mass(*args) ⇒ Object
-
#circle(center, radius, options = {}) ⇒ Object
Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
- #clean_coords(*args) ⇒ Object
-
#clip_polygon(rings, bbox) ⇒ Array<Array<Array<Number>>>
Clips the rings of a Polygon or MultiPolygon to the bbox.
- #clone(*args) ⇒ Object
- #cluster_each(*args) ⇒ Object
- #cluster_reduce(*args) ⇒ Object
- #clusters_dbscan(*args) ⇒ Object
- #clusters_kmeans(*args) ⇒ Object
- #collect(*args) ⇒ Object
- #combine(*args) ⇒ Object
- #compare_coords(*args) ⇒ Object
- #concave(*args) ⇒ Object
- #convex(*args) ⇒ Object
-
#coord_all(geojson) ⇒ Array<Array<Number>>
Get all coordinates from any GeoJSON object.
-
#coord_each(geojson, exclude_wrap_coord: false) {|current_coord, coord_index, feature_index, multi_feature_index, geometry_index| ... } ⇒ void
Iterate over coordinates in any GeoJSON object, similar to Array#each.
-
#coord_reduce(geojson, initial_value = nil, exclude_wrap_coord: false) ⇒ *
Reduce coordinates in any GeoJSON object, similar to Array.reduce(*args) ring in its iteration.
- #coords_to_line(*args) ⇒ Object
- #create_bins(*args) ⇒ Object
- #difference(*args) ⇒ Object
- #directional_mean(*args) ⇒ Object
- #dissolve(*args) ⇒ Object
- #distance_weight(*args) ⇒ Object
- #do_b_box_overlap(*args) ⇒ Object
- #ellipse(*args) ⇒ Object
- #envelope(*args) ⇒ Object
- #explode(geojson) ⇒ Object
-
#feature_each(geojson) {|feature| ... } ⇒ Object
Iterate over features in any GeoJSON object, similar to Array.forEach.
-
#feature_reduce(geojson, initial_value = nil) {|previous_value, feature, feature_index| ... } ⇒ *
Reduce features in any GeoJSON object, similar to Array.reduce(*args).
- #filter_properties(*args) ⇒ Object
-
#find_point(geojson, options = {}) ⇒ Feature<Point>
Finds a particular Point from a GeoJSON using ‘@turf/meta` indexes.
-
#find_segment(geojson, options = {}) ⇒ Feature<LineString>
Finds a particular 2-vertex LineString Segment from a GeoJSON using ‘@turf/meta` indexes.
- #flatten(*args) ⇒ Object
-
#flatten_each(geojson) {|feature, feature_index, multi_feature_index| ... } ⇒ Object
Iterate over flattened features in any GeoJSON object, similar to Array.forEach.
-
#flatten_reduce(geojson, initial_value = nil) {|previous_value, feature, feature_index, multi_feature_index| ... } ⇒ *
Reduce flattened features in any GeoJSON object, similar to Array.reduce(*args).
- #flip(*args) ⇒ Object
- #geojson_rbush(*args) ⇒ Object
-
#geom_each(geojson) {|geom, feature_index, properties, bbox, id| ... } ⇒ Object
Iterate over each geometry in any GeoJSON object, similar to Array.forEach(*args).
-
#geom_reduce(geojson, initial_value = nil) {|previous_value, geom, geom_index, properties, bbox, id| ... } ⇒ *
Reduce geometry in any GeoJSON object, similar to Array.reduce(*args).
-
#geometries(geojson) ⇒ Array<Geometry>
Get all Geometry.
- #get_cluster(*args) ⇒ Object
- #get_midpoint(*args) ⇒ Object
- #great_circle(*args) ⇒ Object
- #hex_grid(*args) ⇒ Object
- #interpolate(*args) ⇒ Object
- #intersect(*args) ⇒ Object
- #is_line_in_poly(*args) ⇒ Object
- #is_line_on_line(*args) ⇒ Object
- #is_multi_point_in_multi_point(*args) ⇒ Object
- #is_multi_point_in_poly(*args) ⇒ Object
- #is_multi_point_on_line(*args) ⇒ Object
- #is_point_in_multi_point(*args) ⇒ Object
-
#is_point_on_line_segment(line_segment_start, line_segment_end, point, exclude_boundary, epsilon = nil) ⇒ Boolean
Determines if a point is on a line segment.
- #is_poly_in_poly(*args) ⇒ Object
- #is_polygon_in_multi_polygon(*args) ⇒ Object
- #isobands(*args) ⇒ Object
- #isolines(*args) ⇒ Object
- #kinks(*args) ⇒ Object
- #line_arc(*args) ⇒ Object
- #line_chunk(*args) ⇒ Object
-
#line_each(geojson) {|current_line, feature_index, multi_feature_index, geometry_index| ... } ⇒ Object
Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries, similar to Array.forEach.
- #line_intersect(*args) ⇒ Object
- #line_offset(*args) ⇒ Object
- #line_overlap(*args) ⇒ Object
-
#line_reduce(geojson, initial_value = nil) {|previous_value, current_line, feature_index, multi_feature_index, geometry_index| ... } ⇒ Object
Reduce features in any GeoJSON object, similar to Array.reduce().
- #line_segment(*args) ⇒ Object
- #line_slice(*args) ⇒ Object
- #line_slice_along(*args) ⇒ Object
- #line_split(*args) ⇒ Object
- #line_to_polygon(*args) ⇒ Object
- #mask(*args) ⇒ Object
- #midpoint(*args) ⇒ Object
- #moran_index(*args) ⇒ Object
- #multi_polygon_to_line(*args) ⇒ Object
- #nearest_neighbor_analysis(*args) ⇒ Object
- #nearest_point(*args) ⇒ Object
- #nearest_point_on_line(*args) ⇒ Object
- #nearest_point_to_line(*args) ⇒ Object
- #p_norm_distance(*args) ⇒ Object
- #planepoint(*args) ⇒ Object
- #point_grid(*args) ⇒ Object
- #point_on_feature(*args) ⇒ Object
- #point_to_line_distance(*args) ⇒ Object
- #point_to_polygon_distance(*args) ⇒ Object
- #points_within_polygon(*args) ⇒ Object
- #polygon_smooth(*args) ⇒ Object
- #polygon_tangents(*args) ⇒ Object
- #polygon_to_line(*args) ⇒ Object
- #polygonize(*args) ⇒ Object
-
#prop_each(geojson) {|current_properties, feature_index| ... } ⇒ Object
Iterate over properties in any GeoJSON object, similar to Array.forEach.
-
#prop_reduce(geojson, initial_value = nil) {|previous_value, current_properties, feature_index| ... } ⇒ Object
Reduce properties in any GeoJSON object into a single value, similar to how Array.reduce works.
- #properties_contains_filter(*args) ⇒ Object
- #quadrat_analysis(*args) ⇒ Object
- #random_line_string(*args) ⇒ Object
- #random_point(*args) ⇒ Object
- #random_polygon(*args) ⇒ Object
- #random_position(*args) ⇒ Object
- #rectangle_grid(*args) ⇒ Object
- #rewind(*args) ⇒ Object
- #rhumb_bearing(*args) ⇒ Object
- #rhumb_destination(*args) ⇒ Object
- #rhumb_distance(*args) ⇒ Object
- #sample(*args) ⇒ Object
- #sector(*args) ⇒ Object
-
#segment_each(geojson) ⇒ Object
Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach(*args) (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
- #segment_reduce(geojson, initial_value = nil) ⇒ Object
- #shortest_path(*args) ⇒ Object
- #simplify(*args) ⇒ Object
- #single_polygon_to_line(*args) ⇒ Object
-
#square(bbox) ⇒ Array<Float>
Takes a bounding box and calculates the minimum square bounding box that would contain the input.
- #square_grid(*args) ⇒ Object
- #standard_deviational_ellipse(*args) ⇒ Object
- #tag(*args) ⇒ Object
- #tesselate(*args) ⇒ Object
- #tin(*args) ⇒ Object
- #to_mercator(*args) ⇒ Object
- #to_wgs84(*args) ⇒ Object
- #transform_rotate(*args) ⇒ Object
- #transform_scale(*args) ⇒ Object
- #transform_translate(*args) ⇒ Object
- #triangle_grid(*args) ⇒ Object
-
#truncate(geojson, options = {}) ⇒ Object
Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
- #truncate_coords(coords, precision, coordinates) ⇒ Object
- #union(*args) ⇒ Object
- #unkink_polygon(*args) ⇒ Object
- #voronoi(*args) ⇒ Object
Instance Method Details
#along(line, distance, options = {}) ⇒ Feature<Point>
Takes a LineString and returns a Point at a specified distance along the line.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/turf/along.rb', line 19 def along(line, distance, = {}) units = [:units] || "kilometers" line = deep_symbolize_keys line travelled = 0 geom = get_geom line coords = geom[:coordinates] coords.each_with_index do |coord, i| break if distance >= travelled && i == coords.length - 1 if travelled >= distance overshot = distance - travelled return point(coord) if overshot.zero? direction = bearing(coord, coords[i - 1]) - 180 interpolated = destination(coord, overshot, direction, { units: units }) return interpolated else travelled += distance(coords[i], coords[i + 1], { units: units }) end end point(coords.last) end |
#angle(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/angle.rb', line 5 def angle(*args) raise NotImplementedError end |
#apply_filter(*args) ⇒ Object
21 22 23 |
# File 'lib/turf/clusters.rb', line 21 def apply_filter(*args) raise NotImplementedError end |
#area(geojson) ⇒ number
Takes one or more features and returns their area in square meters.
12 13 14 15 16 |
# File 'lib/turf/area.rb', line 12 def area(geojson) geom_reduce(geojson, 0) do |value, geom| value + area_calculate_area(geom) end end |
#azimuth_to_bearing(angle) ⇒ number
Converts any azimuth angle from the north line direction (positive clockwise) and returns an angle between -180 and +180 degrees (positive clockwise), 0 being the north line
307 308 309 310 311 312 |
# File 'lib/turf/helpers.rb', line 307 def azimuth_to_bearing(angle) angle = angle.remainder(360) angle -= 360 if angle > 180 angle += 360 if angle < -180 angle end |
#bbox(geojson, options = {}) ⇒ Array<Float>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/turf/bbox.rb', line 19 def bbox(geojson, = {}) # If geojson has a bbox property and options[:recompute] is not true, return the existing bbox return geojson[:bbox] if geojson[:bbox] && [:recompute] != true # Initialize the result array with infinity values result = [Float::INFINITY, Float::INFINITY, -Float::INFINITY, -Float::INFINITY] # Iterate through each coordinate in the GeoJSON object using coord_each coord_each(geojson) do |coord| result[0] = coord[0] if result[0] > coord[0] # minX result[1] = coord[1] if result[1] > coord[1] # minY result[2] = coord[0] if result[2] < coord[0] # maxX result[3] = coord[1] if result[3] < coord[1] # maxY end result end |
#bbox_clip(feature, bbox) ⇒ Feature<LineString, MultiLineString, Polygon, MultiPolygon>
Takes a Feature and a bbox and clips the feature to the bbox using [lineclip](github.com/mapbox/lineclip). May result in degenerate edges when clipping Polygons.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/turf/bbox_clip.rb', line 20 def bbox_clip(feature, bbox) geom = get_geom(feature) type = geom[:type] properties = feature[:type] == "Feature" ? feature[:properties] : {} coords = geom[:coordinates] case type when "LineString", "MultiLineString" lines = [] coords = [coords] if type == "LineString" coords.each do |line| Lineclip.lineclip(line, bbox, lines) end if lines.length == 1 return line_string(lines[0], properties) end multi_line_string(lines, properties) when "Polygon" polygon(clip_polygon(coords, bbox), properties) when "MultiPolygon" multi_polygon( coords.map { |poly| clip_polygon(poly, bbox) }, properties, ) else raise Error, "geometry #{type} not supported" end end |
#bbox_polygon(bbox, options = {}) ⇒ Feature<Polygon>
Takes a bbox and returns an equivalent polygon.
bbox = [0, 0, 10, 10] poly = bbox_polygon(bbox) # addToMap add_to_map = [poly]
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/turf/bbox_polygon.rb', line 17 def bbox_polygon(bbox, = {}) # Convert BBox positions to Numbers # No performance loss for including to_f # https://github.com/Turfjs/turf/issues/1119 west = Float(bbox[0], exception: false) || Float::NAN south = Float(bbox[1], exception: false) || Float::NAN east = Float(bbox[2], exception: false) || Float::NAN north = Float(bbox[3], exception: false) || Float::NAN if bbox.length == 6 raise Error, "@turf/bbox-polygon does not support BBox with 6 positions" end low_left = [west, south] top_left = [west, north] top_right = [east, north] low_right = [east, south] polygon( [[low_left, low_right, top_right, top_left, low_left]], [:properties] || {}, { bbox: bbox, id: [:id] }, ) end |
#bearing(from, to, options = {}) ⇒ number
Takes two points and finds the geographic bearing between them, i.e. the angle measured in degrees from the north line (0 degrees)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/turf/bearing.rb', line 17 def bearing(from, to, = {}) return calculate_final_bearing(from, to) if [:final] coordinates1 = get_coord from coordinates2 = get_coord to lon1 = degrees_to_radians(coordinates1[0]) lon2 = degrees_to_radians(coordinates2[0]) lat1 = degrees_to_radians(coordinates1[1]) lat2 = degrees_to_radians(coordinates2[1]) a = Math.sin(lon2 - lon1) * Math.cos(lat2) b = (Math.cos(lat1) * Math.sin(lat2)) - (Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) radians_to_degrees(Math.atan2(a, b)) end |
#bearing_to_azimuth(bearing) ⇒ number
Converts any bearing angle from the north line direction (positive clockwise) and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line
319 320 321 322 323 |
# File 'lib/turf/helpers.rb', line 319 def bearing_to_azimuth(bearing) angle = bearing.remainder(360) angle += 360 if angle < 0 angle end |
#bezier_spline(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/bezier_spline.rb', line 5 def bezier_spline(*args) raise NotImplementedError end |
#boolean_clockwise(line) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/turf/boolean_clockwise.rb', line 16 def boolean_clockwise(line) ring = get_coords(line) sum = 0 ring.each_cons(2) do |prev, cur| sum += (cur[0] - prev[0]) * (cur[1] + prev[1]) end sum > 0 end |
#boolean_concave(polygon) ⇒ Boolean
Takes a polygon and returns true or false as to whether it is concave or not.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/turf/boolean_concave.rb', line 17 def boolean_concave(polygon) coords = get_geom(polygon)[:coordinates] return false if coords[0].length <= 4 sign = nil n = coords[0].length - 1 (0...n).each do |i| dx1 = coords[0][(i + 2) % n][0] - coords[0][(i + 1) % n][0] dy1 = coords[0][(i + 2) % n][1] - coords[0][(i + 1) % n][1] dx2 = coords[0][i][0] - coords[0][(i + 1) % n][0] dy2 = coords[0][i][1] - coords[0][(i + 1) % n][1] zcrossproduct = (dx1 * dy2) - (dy1 * dx2) if i.zero? sign = zcrossproduct.positive? elsif sign != zcrossproduct.positive? return true end end false end |
#boolean_contains(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_contains.rb', line 5 def boolean_contains(*args) raise NotImplementedError end |
#boolean_crosses(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_crosses.rb', line 5 def boolean_crosses(*args) raise NotImplementedError end |
#boolean_disjoint(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_disjoint.rb', line 5 def boolean_disjoint(*args) raise NotImplementedError end |
#boolean_equal(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_equal.rb', line 5 def boolean_equal(*args) raise NotImplementedError end |
#boolean_intersects(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_intersects.rb', line 5 def boolean_intersects(*args) raise NotImplementedError end |
#boolean_overlap(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_overlap.rb', line 5 def boolean_overlap(*args) raise NotImplementedError end |
#boolean_parallel(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_parallel.rb', line 5 def boolean_parallel(*args) raise NotImplementedError end |
#boolean_point_in_polygon(point, polygon, options = {}) ⇒ boolean
Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can be convex or concave. The function accounts for holes. inside the polygon otherwise false.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/turf/boolean_point_in_polygon.rb', line 18 def boolean_point_in_polygon(point, polygon, = {}) polygon = deep_symbolize_keys(polygon) pt = get_coord(point) geom = get_geom(polygon) type = geom.fetch(:type) bbox = polygon[:bbox] polys = geom.fetch(:coordinates) # Quick elimination if point is not inside bbox return false if bbox && !in_bbox(pt, bbox) # normalize to multipolygon polys = [polys] if type == "Polygon" inside_poly = false polys.each do |poly| # check if it is in the outer ring first next unless in_ring(pt, poly[0], [:ignore_boundary]) in_hole = false # check for the point in any of the holes poly.slice(1, poly.size - 1).each do |hole| if in_ring(pt, hole, ![:ignore_boundary]) in_hole = true end end if !in_hole inside_poly = true end end inside_poly end |
#boolean_point_on_line(point, line, options = nil) ⇒ Boolean
Returns true if a point is on a line. Accepts an optional parameter to ignore the start and end vertices of the linestring.
Useful for dealing with floating points such as lng/lat points
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/turf/boolean_point_on_line.rb', line 20 def boolean_point_on_line(point, line, = nil) ||= {} # Normalize inputs pt_coords = get_coord(point) line_coords = get_coords(line) # Main line_coords.each_cons(2) do |line_segment_start, line_segment_end| ignore_boundary = false if [:ignore_end_vertices] first_segment = (line_coords.first == line_segment_start) last_segment = (line_coords.last == line_segment_end) if first_segment && last_segment ignore_boundary = :both elsif first_segment ignore_boundary = :start elsif last_segment ignore_boundary = :end end end return true if is_point_on_line_segment( line_segment_start, line_segment_end, pt_coords, ignore_boundary, [:epsilon], ) end false end |
#boolean_touches(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_touches.rb', line 5 def boolean_touches(*args) raise NotImplementedError end |
#boolean_valid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_valid.rb', line 5 def boolean_valid(*args) raise NotImplementedError end |
#boolean_within(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/boolean_within.rb', line 5 def boolean_within(*args) raise NotImplementedError end |
#buffer(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/buffer.rb', line 5 def buffer(*args) raise NotImplementedError end |
#center(geojson, options = {}) ⇒ Feature<Point>
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
26 27 28 29 30 31 |
# File 'lib/turf/center.rb', line 26 def center(geojson, = {}) ext = bbox(geojson) x = (ext[0] + ext[2]) / 2.0 y = (ext[1] + ext[3]) / 2.0 point([x, y], [:properties] || {}, ) end |
#center_mean(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/center_mean.rb', line 5 def center_mean(*args) raise NotImplementedError end |
#center_median(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/center_median.rb', line 5 def center_median(*args) raise NotImplementedError end |
#center_of_mass(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/center_of_mass.rb', line 5 def center_of_mass(*args) raise NotImplementedError end |
#centroid(geojson, properties = nil) ⇒ Feature<Point>
Takes one or more features and calculates the centroid using the mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/turf/centroid.rb', line 16 def centroid(geojson, properties = nil) x_sum = 0.0 y_sum = 0.0 len = 0.0 coord_each geojson, exclude_wrap_coord: true do |coord| x_sum += coord[0] y_sum += coord[1] len += 1 end point( [x_sum / len, y_sum / len], properties, ) end |
#circle(center, radius, options = {}) ⇒ Object
Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/turf/circle.rb', line 17 def circle(center, radius, = {}) center = deep_symbolize_keys(center) # default params steps = [:steps] || 64 properties = [:properties] properties ||= !center.is_a?(Array) && center[:type] == "Feature" && center[:properties] properties ||= {} # main coordinates = [] steps.times do |i| coordinates.push(destination(center, radius, (i * -360.0) / steps, ).dig(:geometry, :coordinates)) end coordinates.push(coordinates[0]) polygon([coordinates], properties) end |
#clean_coords(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/clean_coords.rb', line 5 def clean_coords(*args) raise NotImplementedError end |
#clip_polygon(rings, bbox) ⇒ Array<Array<Array<Number>>>
Clips the rings of a Polygon or MultiPolygon to the bbox.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/turf/bbox_clip.rb', line 57 def clip_polygon(rings, bbox) out_rings = [] rings.each do |ring| clipped = Lineclip.polygonclip(ring, bbox) next unless clipped.any? if clipped[0][0] != clipped[-1][0] || clipped[0][1] != clipped[-1][1] clipped.push(clipped[0]) end out_rings.push(clipped) if clipped.length >= 4 end out_rings end |
#clone(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/clone.rb', line 5 def clone(*args) raise NotImplementedError end |
#cluster_each(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/clusters.rb', line 9 def cluster_each(*args) raise NotImplementedError end |
#cluster_reduce(*args) ⇒ Object
13 14 15 |
# File 'lib/turf/clusters.rb', line 13 def cluster_reduce(*args) raise NotImplementedError end |
#clusters_dbscan(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/clusters_dbscan.rb', line 5 def clusters_dbscan(*args) raise NotImplementedError end |
#clusters_kmeans(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/clusters_kmeans.rb', line 5 def clusters_kmeans(*args) raise NotImplementedError end |
#collect(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/collect.rb', line 5 def collect(*args) raise NotImplementedError end |
#collection_of(feature_collection, type, name = nil) ⇒ Object
Enforce expectations about types of FeatureCollection inputs for Turf.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/turf/invariant.rb', line 133 def collection_of(feature_collection, type, name = nil) if !feature_collection raise Error, "No featureCollection passed" end if !name raise Error, ".collectionOf() requires a name" end if feature_collection[:type] != "FeatureCollection" raise Error, "Invalid input to #{name}, FeatureCollection required" end feature_collection[:features].each do |feature| if !feature || feature[:type] != "Feature" || !feature[:geometry] raise Error, "Invalid input to #{name}, Feature with geometry required" end if feature[:geometry][:type] != type raise Error, "Invalid input to #{name}: must be a #{type}, given #{feature[:geometry][:type]}" end end end |
#combine(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/combine.rb', line 5 def combine(*args) raise NotImplementedError end |
#compare_coords(*args) ⇒ Object
45 46 47 |
# File 'lib/turf/boolean_contains.rb', line 45 def compare_coords(*args) raise NotImplementedError end |
#concave(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/concave.rb', line 5 def concave(*args) raise NotImplementedError end |
#contains_number(coordinates) ⇒ Boolean
Checks if coordinates contains a number
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/turf/invariant.rb', line 78 def contains_number(coordinates) if coordinates.length > 1 && coordinates[0].is_a?(Numeric) && coordinates[1].is_a?(Numeric) return true end if coordinates[0].is_a?(Array) && !coordinates[0].empty? return contains_number(coordinates[0]) end raise Error, "coordinates must only contain numbers" end |
#convert_area(area, original_unit = nil, final_unit = nil) ⇒ number
Converts an area from one unit to another.
344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/turf/helpers.rb', line 344 def convert_area(area, original_unit = nil, final_unit = nil) original_unit ||= "meters" final_unit ||= "kilometers" raise Error, "area must be a positive number" unless area >= 0 start_factor = AREA_FACTORS[original_unit] raise Error, "invalid original units" unless start_factor final_factor = AREA_FACTORS[final_unit] raise Error, "invalid final units" unless final_factor (area / start_factor) * final_factor end |
#convert_length(length, original_unit = nil, final_unit = nil) ⇒ number
Converts a length from one unit to another.
365 366 367 368 369 370 371 372 |
# File 'lib/turf/helpers.rb', line 365 def convert_length(length, original_unit = nil, final_unit = nil) original_unit ||= "kilometers" final_unit ||= "kilometers" raise Error, "length must be a positive number" unless length >= 0 radians_to_length(length_to_radians(length, original_unit), final_unit) end |
#convex(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/convex.rb', line 5 def convex(*args) raise NotImplementedError end |
#coord_all(geojson) ⇒ Array<Array<Number>>
Get all coordinates from any GeoJSON object.
142 143 144 145 146 147 148 |
# File 'lib/turf/meta.rb', line 142 def coord_all(geojson) coords = [] coord_each(geojson) do |coord| coords.push(coord) end coords end |
#coord_each(geojson, exclude_wrap_coord: false) {|current_coord, coord_index, feature_index, multi_feature_index, geometry_index| ... } ⇒ void
This method returns an undefined value.
Iterate over coordinates in any GeoJSON object, similar to Array#each.
of LinearRings that wraps the ring in its iteration.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/turf/meta.rb', line 38 def coord_each(geojson, exclude_wrap_coord: false) return if geojson.nil? coord_index = 0 is_geometry_collection = false type = geojson[:type] is_feature_collection = type == "FeatureCollection" is_feature = type == "Feature" stop = is_feature_collection ? geojson[:features].length : 1 (0...stop).each do |feature_index| geometry_maybe_collection = if is_feature_collection geojson[:features][feature_index][:geometry] elsif is_feature geojson[:geometry] else geojson end is_geometry_collection = if geometry_maybe_collection geometry_maybe_collection[:type] == "GeometryCollection" else false end stop_g = is_geometry_collection ? geometry_maybe_collection[:geometries].length : 1 (0...stop_g).each do |geom_index| multi_feature_index = 0 geometry_index = 0 geometry = if is_geometry_collection geometry_maybe_collection[:geometries][geom_index] else geometry_maybe_collection end next if geometry.nil? coords = geometry[:coordinates] geom_type = geometry[:type] wrap_shrink = exclude_wrap_coord && %w[Polygon MultiPolygon].include?(geom_type) ? 1 : 0 case geom_type when "Point" return false if yield(coords, coord_index, feature_index, multi_feature_index, geometry_index) == false coord_index += 1 multi_feature_index += 1 when "LineString", "MultiPoint" coords.each_with_index do |coord, _j| return false if yield(coord, coord_index, feature_index, multi_feature_index, geometry_index) == false coord_index += 1 multi_feature_index += 1 if geom_type == "MultiPoint" end multi_feature_index += 1 if geom_type == "LineString" when "Polygon", "MultiLineString" coords.each_with_index do |coord, _j| (0...(coord.length - wrap_shrink)).each do |k| return false if yield(coord[k], coord_index, feature_index, multi_feature_index, geometry_index) == false coord_index += 1 end multi_feature_index += 1 if geom_type == "MultiLineString" geometry_index += 1 if geom_type == "Polygon" end multi_feature_index += 1 if geom_type == "Polygon" when "MultiPolygon" coords.each_with_index do |coord, _j| geometry_index = 0 coord.each_with_index do |inner_coord, _k| (0...(inner_coord.length - wrap_shrink)).each do |l| if yield(inner_coord[l], coord_index, feature_index, multi_feature_index, geometry_index) == false return false end coord_index += 1 end geometry_index += 1 end multi_feature_index += 1 end when "GeometryCollection" geometry[:geometries].each do |inner_geometry| return false if coord_each(inner_geometry, exclude_wrap_coord: exclude_wrap_coord, &Proc.new) == false end else raise Error, "Unknown Geometry Type" end end end end |
#coord_reduce(geojson, initial_value = nil, exclude_wrap_coord: false) ⇒ *
Reduce coordinates in any GeoJSON object, similar to Array.reduce(*args) ring in its iteration.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/turf/meta.rb', line 157 def coord_reduce(geojson, initial_value = nil, exclude_wrap_coord: false) previous_value = initial_value coord_each( geojson, exclude_wrap_coord: exclude_wrap_coord, ) do |current_coord, coord_index| previous_value = if coord_index.zero? && initial_value.nil? current_coord else yield( previous_value, current_coord, coord_index ) end end previous_value end |
#coords_to_line(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/polygon_to_line.rb', line 9 def coords_to_line(*args) raise NotImplementedError end |
#create_bins(*args) ⇒ Object
17 18 19 |
# File 'lib/turf/clusters.rb', line 17 def create_bins(*args) raise NotImplementedError end |
#degrees_to_radians(degrees) ⇒ number
Converts an angle in degrees to radians
217 218 219 220 |
# File 'lib/turf/helpers.rb', line 217 def degrees_to_radians(degrees) radians = degrees.remainder(360) radians * Math::PI / 180 end |
#destination(origin, distance, bearing, options = {}) ⇒ Feature<Point>
Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers; and bearing in degrees. This uses the Haversine formula to account for global curvature.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/turf/destination.rb', line 19 def destination(origin, distance, bearing, = {}) coordinates1 = get_coord origin longitude1 = degrees_to_radians coordinates1[0] latitude1 = degrees_to_radians coordinates1[1] bearing_radians = degrees_to_radians bearing radians = length_to_radians distance, [:units] latitude2 = Math.asin((Math.sin(latitude1) * Math.cos(radians)) + (Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearing_radians))) longitude2 = longitude1 + Math.atan2( Math.sin(bearing_radians) * Math.sin(radians) * Math.cos(latitude1), Math.cos(radians) - (Math.sin(latitude1) * Math.sin(latitude2)), ) lng = radians_to_degrees(longitude2) lat = radians_to_degrees(latitude2) point([lng, lat], [:properties]) end |
#difference(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/difference.rb', line 5 def difference(*args) raise NotImplementedError end |
#directional_mean(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/directional_mean.rb', line 5 def directional_mean(*args) raise NotImplementedError end |
#dissolve(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/dissolve.rb', line 5 def dissolve(*args) raise NotImplementedError end |
#distance(from, to, options = {}) ⇒ number
Calculates the distance between two points in degrees, radians, miles, or kilometers. This uses the Haversine formula to account for global curvature.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/turf/distance.rb', line 17 def distance(from, to, = {}) coordinates1 = get_coord from coordinates2 = get_coord to d_lat = degrees_to_radians coordinates2[1] - coordinates1[1] d_lon = degrees_to_radians coordinates2[0] - coordinates1[0] lat1 = degrees_to_radians coordinates1[1] lat2 = degrees_to_radians coordinates2[1] a = ( (Math.sin(d_lat / 2.0)**2) + ((Math.sin(d_lon / 2.0)**2) * Math.cos(lat1) * Math.cos(lat2)) ) radians_to_length( 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1 - a), ), [:units], ) end |
#distance_weight(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/distance_weight.rb', line 9 def distance_weight(*args) raise NotImplementedError end |
#do_b_box_overlap(*args) ⇒ Object
41 42 43 |
# File 'lib/turf/boolean_contains.rb', line 41 def do_b_box_overlap(*args) raise NotImplementedError end |
#ellipse(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/ellipse.rb', line 5 def ellipse(*args) raise NotImplementedError end |
#envelope(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/envelope.rb', line 5 def envelope(*args) raise NotImplementedError end |
#explode(geojson) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/turf/explode.rb', line 8 def explode(geojson) points = [] if geojson[:type] == "FeatureCollection" feature_each(geojson) do |feature| coord_each(feature) do |coord| points << point(coord, feature[:properties]) end end elsif geojson[:type] == "Feature" coord_each(geojson) do |coord| points << point(coord, geojson[:properties]) end else coord_each(geojson) do |coord| points << point(coord) end end feature_collection(points) end |
#feature(geom, properties = nil, options = {}) ⇒ Feature
Wraps a GeoJSON Geometry in a GeoJSON Feature.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/turf/helpers.rb', line 51 def feature(geom, properties = nil, = {}) feat = { type: "Feature", properties: properties || {}, geometry: geom } feat[:id] = [:id] if [:id] feat[:bbox] = [:bbox] if [:bbox] feat end |
#feature_collection(features, options = {}) ⇒ FeatureCollection
Takes one or more Features and creates a FeatureCollection.
69 70 71 72 73 74 75 76 |
# File 'lib/turf/helpers.rb', line 69 def feature_collection(features, = {}) fc = { type: "FeatureCollection" } fc[:id] = [:id] if [:id] fc[:bbox] = [:bbox] if [:bbox] fc[:features] = features fc end |
#feature_each(geojson) {|feature| ... } ⇒ Object
Iterate over features in any GeoJSON object, similar to Array.forEach.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/turf/meta.rb', line 272 def feature_each(geojson, &block) return unless geojson features = [] geojson = deep_symbolize_keys! geojson case geojson[:type] when "Feature" features.push geojson when "FeatureCollection" features.push(*geojson[:features]) end features.each_with_index(&block) end |
#feature_of(feature = nil, type = nil, name = nil) ⇒ Object
Enforce expectations about types of Feature inputs for Turf.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/turf/invariant.rb', line 112 def feature_of(feature = nil, type = nil, name = nil) if !feature raise Error, "No feature passed" end if !name raise Error, ".featureOf() requires a name" end if !feature || feature[:type] != "Feature" || !feature[:geometry] raise Error, "Invalid input to #{name}, Feature with geometry required" end return unless feature[:geometry][:type] != type raise Error, "Invalid input to #{name}: must be a #{type}, given #{feature[:geometry][:type]}" end |
#feature_reduce(geojson, initial_value = nil) {|previous_value, feature, feature_index| ... } ⇒ *
Reduce features in any GeoJSON object, similar to Array.reduce(*args).
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/turf/meta.rb', line 295 def feature_reduce(geojson, initial_value = nil) previous_value = initial_value feature_each( geojson, ) do |feature, feature_index| previous_value = if feature_index.zero? && initial_value.nil? feature else yield( previous_value, feature, feature_index ) end end previous_value end |
#filter_properties(*args) ⇒ Object
29 30 31 |
# File 'lib/turf/clusters.rb', line 29 def filter_properties(*args) raise NotImplementedError end |
#find_point(geojson, options = {}) ⇒ Feature<Point>
Finds a particular Point from a GeoJSON using ‘@turf/meta` indexes.
Negative indexes are permitted.
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/turf/meta.rb', line 677 def find_point(geojson, = {}) ||= {} raise "options is invalid" unless .is_a?(Hash) feature_index = [:feature_index] || 0 multi_feature_index = [:multi_feature_index] || 0 geometry_index = [:geometry_index] || 0 coord_index = [:coord_index] || 0 properties = [:properties] geometry = nil case geojson[:type] when "FeatureCollection" feature_index = geojson[:features].length + feature_index if feature_index < 0 properties ||= geojson[:features][feature_index][:properties] geometry = geojson[:features][feature_index][:geometry] when "Feature" properties ||= geojson[:properties] geometry = geojson[:geometry] when "Point", "MultiPoint" return nil when "LineString", "Polygon", "MultiLineString", "MultiPolygon" geometry = geojson else raise "geojson is invalid" end return nil if geometry.nil? coords = geometry[:coordinates] case geometry[:type] when "Point" point(coords, properties, ) when "MultiPoint" multi_feature_index = coords.length + multi_feature_index if multi_feature_index < 0 point(coords[multi_feature_index], properties, ) when "LineString" coord_index = coords.length + coord_index if coord_index < 0 point(coords[coord_index], properties, ) when "Polygon" geometry_index = coords.length + geometry_index if geometry_index < 0 coord_index = coords[geometry_index].length + coord_index if coord_index < 0 point(coords[geometry_index][coord_index], properties, ) when "MultiLineString" multi_feature_index = coords.length + multi_feature_index if multi_feature_index < 0 coord_index = coords[multi_feature_index].length + coord_index if coord_index < 0 point(coords[multi_feature_index][coord_index], properties, ) when "MultiPolygon" multi_feature_index = coords.length + multi_feature_index if multi_feature_index < 0 geometry_index = coords[multi_feature_index].length + geometry_index if geometry_index < 0 coord_index = coords[multi_feature_index][geometry_index].length - coord_index if coord_index < 0 point(coords[multi_feature_index][geometry_index][coord_index], properties, ) else raise "geojson is invalid" end end |
#find_segment(geojson, options = {}) ⇒ Feature<LineString>
Finds a particular 2-vertex LineString Segment from a GeoJSON using ‘@turf/meta` indexes.
Negative indexes are permitted. Point & MultiPoint will always return null.
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/turf/meta.rb', line 586 def find_segment(geojson, = {}) ||= {} raise "options is invalid" unless .is_a?(Hash) feature_index = .fetch(:feature_index, 0) multi_feature_index = .fetch(:multi_feature_index, 0) geometry_index = .fetch(:geometry_index, 0) segment_index = .fetch(:segment_index, 0) properties = [:properties] geometry = nil case geojson[:type] when "FeatureCollection" feature_index += geojson[:features].length if feature_index < 0 properties ||= geojson[:features][feature_index][:properties] geometry = geojson[:features][feature_index][:geometry] when "Feature" properties ||= geojson[:properties] geometry = geojson[:geometry] when "Point", "MultiPoint" return nil when "LineString", "Polygon", "MultiLineString", "MultiPolygon" geometry = geojson else raise "geojson is invalid" end return nil if geometry.nil? coords = geometry[:coordinates] case geometry[:type] when "Point", "MultiPoint" nil when "LineString" segment_index += coords.length - 1 if segment_index < 0 line_string([coords[segment_index], coords[segment_index + 1]], properties, ) when "Polygon" geometry_index += coords.length if geometry_index < 0 segment_index += coords[geometry_index].length - 1 if segment_index < 0 line_string([coords[geometry_index][segment_index], coords[geometry_index][segment_index + 1]], properties, ) when "MultiLineString" multi_feature_index += coords.length if multi_feature_index < 0 segment_index += coords[multi_feature_index].length - 1 if segment_index < 0 line_string([coords[multi_feature_index][segment_index], coords[multi_feature_index][segment_index + 1]], properties, ) when "MultiPolygon" multi_feature_index += coords.length if multi_feature_index < 0 geometry_index += coords[multi_feature_index].length if geometry_index < 0 segment_index += coords[multi_feature_index][geometry_index].length - 1 if segment_index < 0 line_string( [coords[multi_feature_index][geometry_index][segment_index], coords[multi_feature_index][geometry_index][segment_index + 1],], properties, ) else raise "geojson is invalid" end end |
#flatten(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/flatten.rb', line 5 def flatten(*args) raise NotImplementedError end |
#flatten_each(geojson) {|feature, feature_index, multi_feature_index| ... } ⇒ Object
Iterate over flattened features in any GeoJSON object, similar to Array.forEach.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/turf/meta.rb', line 322 def flatten_each(geojson) geom_each(geojson) do |geometry, feature_index, properties, bbox, id| if geometry.nil? next yield( feature(nil, properties, bbox: bbox, id: id), feature_index, 0 ) end case geometry[:type] when "Point", "LineString", "Polygon" yield( feature(geometry, properties, bbox: bbox, id: id), feature_index, 0 ) when "MultiPoint", "MultiLineString", "MultiPolygon" geom_type = geometry[:type].sub(/^Multi/, "") geometry[:coordinates].each_with_index do |coordinate, multi_feature_index| geom = { type: geom_type, coordinates: coordinate } yield( feature(geom, properties), feature_index, multi_feature_index ) end end end end |
#flatten_reduce(geojson, initial_value = nil) {|previous_value, feature, feature_index, multi_feature_index| ... } ⇒ *
Reduce flattened features in any GeoJSON object, similar to Array.reduce(*args).
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/turf/meta.rb', line 365 def flatten_reduce(geojson, initial_value = nil) previous_value = initial_value flatten_each( geojson, ) do |feature, feature_index, multi_feature_index| previous_value = if feature_index.zero? && multi_feature_index.zero? && initial_value.nil? feature else yield( previous_value, feature, feature_index, multi_feature_index ) end end previous_value end |
#flip(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/flip.rb', line 5 def flip(*args) raise NotImplementedError end |
#geojson_rbush(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/geojson_rbush.rb', line 5 def geojson_rbush(*args) raise NotImplementedError end |
#geojson_type(value = nil, type = nil, name = nil) ⇒ Object
Enforce expectations about types of GeoJSON objects for Turf.
96 97 98 99 100 101 102 103 104 |
# File 'lib/turf/invariant.rb', line 96 def geojson_type(value = nil, type = nil, name = nil) if !type || !name raise Error, "type and name required" end return unless !value || value[:type] != type raise Error, "Invalid input to #{name}: must be a #{type}, given #{value[:type]}" end |
#geom_each(geojson) {|geom, feature_index, properties, bbox, id| ... } ⇒ Object
Iterate over each geometry in any GeoJSON object, similar to Array.forEach(*args)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/turf/meta.rb', line 187 def geom_each(geojson) return unless geojson geojson = deep_symbolize_keys! geojson entries = [] case geojson[:type] when "FeatureCollection" geojson[:features].each do |feature| entries.push [feature[:geometry], feature[:properties], feature[:bbox], feature[:id]] end when "Feature" entries.push [geojson[:geometry], geojson[:properties], geojson[:bbox], geojson[:id]] else entries.push [geojson, {}, nil, nil] end entry_index = -1 # flatten GeometryCollection entries.each do |entry| geometry, properties, bbox, id = entry if geometry.nil? || geometry[:type] != "GeometryCollection" yield(geometry, (entry_index += 1), properties, bbox, id) else geometry[:geometries].each do |sub_geometry| yield(sub_geometry, (entry_index += 1), properties, bbox, id) end end end end |
#geom_reduce(geojson, initial_value = nil) {|previous_value, geom, geom_index, properties, bbox, id| ... } ⇒ *
Reduce geometry in any GeoJSON object, similar to Array.reduce(*args).
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/turf/meta.rb', line 231 def geom_reduce(geojson, initial_value = nil) previous_value = initial_value geom_each( geojson, ) do |geom, geom_index, properties, bbox, id| previous_value = if geom_index.zero? && initial_value.nil? geom else yield( previous_value, geom, geom_index, properties, bbox, id ) end end previous_value end |
#geometries(geojson) ⇒ Array<Geometry>
Get all Geometry
258 259 260 261 262 263 264 |
# File 'lib/turf/meta.rb', line 258 def geometries(geojson) geometries = [] geom_each(geojson) do |geometry| geometries.push(geometry) end geometries end |
#geometry_collection(geometries, properties = nil, options = {}) ⇒ Feature<GeometryCollection>
Creates a Feature based on a coordinate array. Properties can be added optionally.
85 86 87 88 89 90 91 92 |
# File 'lib/turf/helpers.rb', line 85 def geometry_collection(geometries, properties = nil, = {}) geom = { type: "GeometryCollection", geometries: geometries } feature(geom, properties, ) end |
#get_cluster(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/clusters.rb', line 5 def get_cluster(*args) raise NotImplementedError end |
#get_coord(coord) ⇒ Array
Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/turf/invariant.rb', line 13 def get_coord(coord) if !coord raise Error, "coord is required" end is_numeric = ->(i) { i.is_a? Numeric } if coord.is_a?(Array) && coord.length >= 2 && coord.all?(&is_numeric) return coord end if coord.is_a? Hash coord = deep_symbolize_keys(coord) is_feature = coord[:type] == "Feature" if is_feature && coord.dig(:geometry, :type) == "Point" return coord[:geometry][:coordinates] end if coord[:type] == "Point" return coord[:coordinates] end end raise Error, "coord must be GeoJSON Point or an Array of numbers" end |
#get_coords(coords) ⇒ Array
Unwrap coordinates from a Feature, Geometry Object or an Array
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/turf/invariant.rb', line 54 def get_coords(coords) if coords.is_a?(Array) return coords end coords = deep_symbolize_keys coords # Feature if coords.is_a?(Hash) && coords[:type] == "Feature" if coords[:geometry] return coords[:geometry][:coordinates] end elsif coords.is_a?(Hash) && coords[:coordinates] # Geometry return coords[:coordinates] end raise Error, "coords must be GeoJSON Feature, Geometry Object or an Array" end |
#get_geom(geojson) ⇒ Geometry|null
Get Geometry from Feature or Geometry Object
43 44 45 46 47 48 |
# File 'lib/turf/invariant.rb', line 43 def get_geom(geojson) geojson = deep_symbolize_keys geojson return geojson[:geometry] if geojson[:type] == "Feature" geojson end |
#get_midpoint(*args) ⇒ Object
49 50 51 |
# File 'lib/turf/boolean_contains.rb', line 49 def get_midpoint(*args) raise NotImplementedError end |
#get_type(geojson, _name = "geojson") ⇒ String
Get GeoJSON object’s type, Geometry type is prioritize.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/turf/invariant.rb', line 159 def get_type(geojson, _name = "geojson") geojson = deep_symbolize_keys geojson if geojson[:type] == "FeatureCollection" return "FeatureCollection" end if geojson[:type] == "GeometryCollection" return "GeometryCollection" end if geojson[:type] == "Feature" && geojson[:geometry] return geojson[:geometry][:type] end geojson[:type] end |
#great_circle(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/great_circle.rb', line 5 def great_circle(*args) raise NotImplementedError end |
#hex_grid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/hex_grid.rb', line 5 def hex_grid(*args) raise NotImplementedError end |
#interpolate(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/interpolate.rb', line 5 def interpolate(*args) raise NotImplementedError end |
#intersect(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/intersect.rb', line 5 def intersect(*args) raise NotImplementedError end |
#is_line_in_poly(*args) ⇒ Object
33 34 35 |
# File 'lib/turf/boolean_contains.rb', line 33 def is_line_in_poly(*args) raise NotImplementedError end |
#is_line_on_line(*args) ⇒ Object
29 30 31 |
# File 'lib/turf/boolean_contains.rb', line 29 def is_line_on_line(*args) raise NotImplementedError end |
#is_multi_point_in_multi_point(*args) ⇒ Object
17 18 19 |
# File 'lib/turf/boolean_contains.rb', line 17 def is_multi_point_in_multi_point(*args) raise NotImplementedError end |
#is_multi_point_in_poly(*args) ⇒ Object
25 26 27 |
# File 'lib/turf/boolean_contains.rb', line 25 def is_multi_point_in_poly(*args) raise NotImplementedError end |
#is_multi_point_on_line(*args) ⇒ Object
21 22 23 |
# File 'lib/turf/boolean_contains.rb', line 21 def is_multi_point_on_line(*args) raise NotImplementedError end |
#is_number(num) ⇒ boolean
Checks if the input is a number.
290 291 292 |
# File 'lib/turf/helpers.rb', line 290 def is_number(num) num.is_a?(Numeric) end |
#is_object(input) ⇒ boolean
Checks if the input is an object.
298 299 300 |
# File 'lib/turf/helpers.rb', line 298 def is_object(input) input.is_a?(Hash) && !input.is_a?(Array) end |
#is_point_in_multi_point(*args) ⇒ Object
13 14 15 |
# File 'lib/turf/boolean_contains.rb', line 13 def is_point_in_multi_point(*args) raise NotImplementedError end |
#is_point_on_line_segment(line_segment_start, line_segment_end, point, exclude_boundary, epsilon = nil) ⇒ Boolean
Determines if a point is on a line segment.
Can be true, false, or one of “start”, “end”, or “both”. comparison (useful for floating-point coordinates).
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/turf/boolean_point_on_line.rb', line 64 def is_point_on_line_segment(line_segment_start, line_segment_end, point, exclude_boundary, epsilon = nil) x, y = point x1, y1 = line_segment_start x2, y2 = line_segment_end dxc = x - x1 dyc = y - y1 dxl = x2 - x1 dyl = y2 - y1 cross = (dxc * dyl) - (dyc * dxl) if epsilon return false if cross.abs > epsilon elsif cross != 0 return false end # Special case: zero-length line segments if dxl == 0 && dyl == 0 return false if exclude_boundary return point == line_segment_start end case exclude_boundary when false if dxl.abs >= dyl.abs if dxl > 0 x.between?(x1, x2) else x.between?(x2, x1) end else (if dyl > 0 y.between?(y1, y2) else y.between?(y2, y1) end) end when :start if dxl.abs >= dyl.abs dxl > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1 else (dyl > 0 ? y1 < y && y <= y2 : y2 < y && y < y1) end when :end if dxl.abs >= dyl.abs dxl > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1 else (dyl > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1) end when :both if dxl.abs >= dyl.abs dxl > 0 ? x1 < x && x < x2 : x2 < x && x < x1 else (dyl > 0 ? y1 < y && y < y2 : y2 < y && y < y1) end else false end end |
#is_poly_in_poly(*args) ⇒ Object
37 38 39 |
# File 'lib/turf/boolean_contains.rb', line 37 def is_poly_in_poly(*args) raise NotImplementedError end |
#is_polygon_in_multi_polygon(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/boolean_contains.rb', line 9 def is_polygon_in_multi_polygon(*args) raise NotImplementedError end |
#isobands(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/isobands.rb', line 5 def isobands(*args) raise NotImplementedError end |
#isolines(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/isolines.rb', line 5 def isolines(*args) raise NotImplementedError end |
#kinks(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/kinks.rb', line 5 def kinks(*args) raise NotImplementedError end |
#length(geojson, options = {}) ⇒ number
Takes a GeoJSON and measures its length in the specified units, (Multi)Point ‘s distance are ignored.
15 16 17 18 19 20 21 22 23 |
# File 'lib/turf/length.rb', line 15 def length(geojson, = {}) geojson = deep_symbolize_keys(geojson) geojson = feature(geojson) if geojson[:geometry].nil? segment_reduce(geojson, 0) do |previous_value, segment| previous_value ||= 0 coords = segment.dig(:geometry, :coordinates) previous_value + distance(coords[0], coords[1], ) end end |
#length_to_radians(distance, units = nil) ⇒ number
Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
242 243 244 245 246 247 248 |
# File 'lib/turf/helpers.rb', line 242 def length_to_radians(distance, units = nil) units ||= "kilometers" factor = FACTORS[units] raise Error, "#{units} units is invalid" unless factor distance / factor end |
#line_arc(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_arc.rb', line 5 def line_arc(*args) raise NotImplementedError end |
#line_chunk(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_chunk.rb', line 5 def line_chunk(*args) raise NotImplementedError end |
#line_each(geojson) {|current_line, feature_index, multi_feature_index, geometry_index| ... } ⇒ Object
Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries, similar to Array.forEach. any GeoJSON object
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/turf/meta.rb', line 505 def line_each(geojson) flatten_each(geojson) do |feature, feature_index, multi_feature_index| next unless feature[:geometry] type = feature[:geometry][:type] coords = feature[:geometry][:coordinates] case type when "LineString" yield(feature, feature_index, multi_feature_index, 0, 0) when "Polygon" coords.each_with_index do |ring, geometry_index| yield( feature({ type: "LineString", coordinates: ring }, feature[:properties]), feature_index, multi_feature_index, geometry_index ) end end end end |
#line_intersect(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_intersect.rb', line 5 def line_intersect(*args) raise NotImplementedError end |
#line_offset(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_offset.rb', line 5 def line_offset(*args) raise NotImplementedError end |
#line_overlap(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_overlap.rb', line 5 def line_overlap(*args) raise NotImplementedError end |
#line_reduce(geojson, initial_value = nil) {|previous_value, current_line, feature_index, multi_feature_index, geometry_index| ... } ⇒ Object
Reduce features in any GeoJSON object, similar to Array.reduce(). any GeoJSON object of the callback, or initial_value, if supplied.
540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/turf/meta.rb', line 540 def line_reduce(geojson, initial_value = nil) previous_value = initial_value line_each(geojson) do |current_line, feature_index, multi_feature_index, geometry_index| previous_value = if feature_index.zero? && initial_value.nil? current_line else yield(previous_value, current_line, feature_index, multi_feature_index, geometry_index) end end previous_value end |
#line_segment(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_segment.rb', line 5 def line_segment(*args) raise NotImplementedError end |
#line_slice(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_slice.rb', line 5 def line_slice(*args) raise NotImplementedError end |
#line_slice_along(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_slice_along.rb', line 5 def line_slice_along(*args) raise NotImplementedError end |
#line_split(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_split.rb', line 5 def line_split(*args) raise NotImplementedError end |
#line_string(coordinates, properties = nil, options = {}) ⇒ Feature<LineString>
Creates a LineString Feature from an Array of Positions.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/turf/helpers.rb', line 116 def line_string(coordinates, properties = nil, = {}) if coordinates.size < 2 raise Error, "coordinates must be an array of two or more positions" end geom = { type: "LineString", coordinates: coordinates } feature(geom, properties, ) end |
#line_strings(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<LineString>
Creates a FeatureCollection from an Array of LineString coordinates.
257 258 259 260 |
# File 'lib/turf/helpers.rb', line 257 def line_strings(coordinates, properties = nil, = {}) features = coordinates.map { |coords| line_string(coords, properties) } feature_collection(features, ) end |
#line_to_polygon(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/line_to_polygon.rb', line 5 def line_to_polygon(*args) raise NotImplementedError end |
#mask(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/mask.rb', line 5 def mask(*args) raise NotImplementedError end |
#midpoint(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/midpoint.rb', line 5 def midpoint(*args) raise NotImplementedError end |
#moran_index(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/moran_index.rb', line 5 def moran_index(*args) raise NotImplementedError end |
#multi_line_string(coordinates, properties = nil, options = {}) ⇒ Feature<MultiLineString>
Creates a Feature<MultiLineString> based on a coordinate array. Properties can be added optionally.
194 195 196 197 198 199 200 |
# File 'lib/turf/helpers.rb', line 194 def multi_line_string(coordinates, properties = nil, = {}) geom = { type: "MultiLineString", coordinates: coordinates } feature(geom, properties, ) end |
#multi_point(coordinates, properties = nil, options = {}) ⇒ Feature<MultiPoint>
Creates a Feature based on a coordinate array. Properties can be added optionally.
100 101 102 103 104 105 106 107 |
# File 'lib/turf/helpers.rb', line 100 def multi_point(coordinates, properties = nil, = {}) geom = { type: "MultiPoint", coordinates: coordinates } feature(geom, properties, ) end |
#multi_polygon(coordinates, properties = nil, options = {}) ⇒ Feature<MultiPolygon>
Creates a Feature<MultiPolygon> based on a coordinate array. Properties can be added optionally.
179 180 181 182 183 184 185 |
# File 'lib/turf/helpers.rb', line 179 def multi_polygon(coordinates, properties = nil, = {}) geom = { type: "MultiPolygon", coordinates: coordinates } feature(geom, properties, ) end |
#multi_polygon_to_line(*args) ⇒ Object
13 14 15 |
# File 'lib/turf/polygon_to_line.rb', line 13 def multi_polygon_to_line(*args) raise NotImplementedError end |
#nearest_neighbor_analysis(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/nearest_neighbor_analysis.rb', line 5 def nearest_neighbor_analysis(*args) raise NotImplementedError end |
#nearest_point(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/nearest_point.rb', line 5 def nearest_point(*args) raise NotImplementedError end |
#nearest_point_on_line(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/nearest_point_on_line.rb', line 5 def nearest_point_on_line(*args) raise NotImplementedError end |
#nearest_point_to_line(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/nearest_point_to_line.rb', line 5 def nearest_point_to_line(*args) raise NotImplementedError end |
#p_norm_distance(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/distance_weight.rb', line 5 def p_norm_distance(*args) raise NotImplementedError end |
#planepoint(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/planepoint.rb', line 5 def planepoint(*args) raise NotImplementedError end |
#point(coordinates, properties = nil, options = {}) ⇒ Feature<Point>
Creates a Point Feature from a Position.
135 136 137 138 139 140 141 |
# File 'lib/turf/helpers.rb', line 135 def point(coordinates, properties = nil, = {}) geom = { type: "Point", coordinates: coordinates } feature(geom, properties, ) end |
#point_grid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/point_grid.rb', line 5 def point_grid(*args) raise NotImplementedError end |
#point_on_feature(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/point_on_feature.rb', line 5 def point_on_feature(*args) raise NotImplementedError end |
#point_to_line_distance(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/point_to_line_distance.rb', line 5 def point_to_line_distance(*args) raise NotImplementedError end |
#point_to_polygon_distance(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/point_to_polygon_distance.rb', line 5 def point_to_polygon_distance(*args) raise NotImplementedError end |
#points(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<Point>
Creates a Point FeatureCollection from an Array of Point coordinates.
269 270 271 272 |
# File 'lib/turf/helpers.rb', line 269 def points(coordinates, properties = nil, = {}) features = coordinates.map { |coords| point(coords, properties) } feature_collection(features, ) end |
#points_within_polygon(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/points_within_polygon.rb', line 5 def points_within_polygon(*args) raise NotImplementedError end |
#polygon(coordinates, properties = nil, options = {}) ⇒ Feature<Polygon>
Creates a Polygon Feature from an Array of LinearRings.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/turf/helpers.rb', line 150 def polygon(coordinates, properties = nil, = {}) coordinates.each do |ring| if ring.size < 4 raise( Error, "Each LinearRing of a Polygon must have 4 or more Positions.", ) end ring.last.each_with_index do |number, idx| if ring.first[idx] != number raise Error, "First and last Position are not equivalent." end end end geom = { type: "Polygon", coordinates: coordinates } feature(geom, properties, ) end |
#polygon_smooth(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/polygon_smooth.rb', line 5 def polygon_smooth(*args) raise NotImplementedError end |
#polygon_tangents(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/polygon_tangents.rb', line 5 def polygon_tangents(*args) raise NotImplementedError end |
#polygon_to_line(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/polygon_to_line.rb', line 5 def polygon_to_line(*args) raise NotImplementedError end |
#polygonize(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/polygonize.rb', line 5 def polygonize(*args) raise NotImplementedError end |
#polygons(coordinates, properties = nil, options = {}) ⇒ FeatureCollection<Polygon>
Creates a Polygon FeatureCollection from an Array of Polygon coordinates.
281 282 283 284 |
# File 'lib/turf/helpers.rb', line 281 def polygons(coordinates, properties = nil, = {}) features = coordinates.map { |coords| polygon(coords, properties) } feature_collection(features, ) end |
#prop_each(geojson) {|current_properties, feature_index| ... } ⇒ Object
Iterate over properties in any GeoJSON object, similar to Array.forEach.
460 461 462 463 464 465 466 467 468 469 |
# File 'lib/turf/meta.rb', line 460 def prop_each(geojson) case geojson[:type] when "FeatureCollection" geojson[:features].each_with_index do |feature, i| break if yield(feature[:properties], i) == false end when "Feature" yield(geojson[:properties], 0) end end |
#prop_reduce(geojson, initial_value = nil) {|previous_value, current_properties, feature_index| ... } ⇒ Object
Reduce properties in any GeoJSON object into a single value, similar to how Array.reduce works. However, in this case we lazily run the reduction, so an array of all properties is unnecessary. of the callback, or initial_value, if supplied.
482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/turf/meta.rb', line 482 def prop_reduce(geojson, initial_value = nil) previous_value = initial_value prop_each(geojson) do |current_properties, feature_index| previous_value = if feature_index.zero? && initial_value.nil? current_properties else yield(previous_value, current_properties, feature_index) end end previous_value end |
#properties_contains_filter(*args) ⇒ Object
25 26 27 |
# File 'lib/turf/clusters.rb', line 25 def properties_contains_filter(*args) raise NotImplementedError end |
#quadrat_analysis(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/quadrat_analysis.rb', line 5 def quadrat_analysis(*args) raise NotImplementedError end |
#radians_to_degrees(radians) ⇒ number
Converts an angle in radians to degrees
208 209 210 211 |
# File 'lib/turf/helpers.rb', line 208 def radians_to_degrees(radians) degrees = radians.remainder(2 * Math::PI) degrees * 180 / Math::PI end |
#radians_to_length(radians, units = nil) ⇒ number
Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit. Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
228 229 230 231 232 233 234 |
# File 'lib/turf/helpers.rb', line 228 def radians_to_length(radians, units = nil) units ||= "kilometers" factor = FACTORS[units] raise Error, "#{units} units is invalid" unless factor radians * factor end |
#random_line_string(*args) ⇒ Object
17 18 19 |
# File 'lib/turf/random.rb', line 17 def random_line_string(*args) raise NotImplementedError end |
#random_point(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/random.rb', line 9 def random_point(*args) raise NotImplementedError end |
#random_polygon(*args) ⇒ Object
13 14 15 |
# File 'lib/turf/random.rb', line 13 def random_polygon(*args) raise NotImplementedError end |
#random_position(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/random.rb', line 5 def random_position(*args) raise NotImplementedError end |
#rectangle_grid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/rectangle_grid.rb', line 5 def rectangle_grid(*args) raise NotImplementedError end |
#rewind(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/rewind.rb', line 5 def rewind(*args) raise NotImplementedError end |
#rhumb_bearing(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/rhumb_bearing.rb', line 5 def rhumb_bearing(*args) raise NotImplementedError end |
#rhumb_destination(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/rhumb_destination.rb', line 5 def rhumb_destination(*args) raise NotImplementedError end |
#rhumb_distance(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/rhumb_distance.rb', line 5 def rhumb_distance(*args) raise NotImplementedError end |
#round(num, precision = 0) ⇒ number
Rounds a number to a specified precision
330 331 332 333 334 335 336 |
# File 'lib/turf/helpers.rb', line 330 def round(num, precision = 0) if !precision.is_a?(Numeric) || precision < 0 raise Error, "invalid precision" end num.round(precision) end |
#sample(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/sample.rb', line 5 def sample(*args) raise NotImplementedError end |
#sector(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/sector.rb', line 5 def sector(*args) raise NotImplementedError end |
#segment_each(geojson) ⇒ Object
Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach(*args) (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/turf/meta.rb', line 391 def segment_each(geojson) flatten_each(geojson) do |feature, feature_index, multi_feature_index| # Exclude null Geometries next if feature[:geometry].nil? # (Multi)Point geometries do not contain segments therefore they are ignored during this operation. type = feature[:geometry][:type] next if %w[Point MultiPoint].include?(type) segment_index = 0 # Generate 2-vertex line segments previous_coords = nil previous_feature_index = 0 previous_multi_index = 0 prev_geom_index = 0 coord_each(feature) do |current_coord, _coord_index, _feature_index_coord, multi_part_index_coord, geometry_index| # Simulating a meta.coord_reduce(*args) since `reduce` operations cannot be stopped by returning `false` if previous_coords.nil? || feature_index > previous_feature_index || multi_part_index_coord > previous_multi_index || geometry_index > prev_geom_index previous_coords = current_coord previous_feature_index = feature_index previous_multi_index = multi_part_index_coord prev_geom_index = geometry_index segment_index = 0 next end segment = line_string([previous_coords, current_coord], feature[:properties]) next unless yield(segment, feature_index, multi_feature_index, geometry_index, segment_index) segment_index += 1 previous_coords = current_coord end end end |
#segment_reduce(geojson, initial_value = nil) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/turf/meta.rb', line 431 def segment_reduce(geojson, initial_value = nil) previous_value = initial_value started = false segment_each(geojson) do |segment, feature_index, multifeature_index, geometry_index, segment_index| previous_value = if !started && initial_value.nil? segment else yield( previous_value, segment, feature_index, multifeature_index, geometry_index, segment_index ) end started = true end previous_value end |
#shortest_path(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/shortest_path.rb', line 5 def shortest_path(*args) raise NotImplementedError end |
#simplify(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/simplify.rb', line 5 def simplify(*args) raise NotImplementedError end |
#single_polygon_to_line(*args) ⇒ Object
17 18 19 |
# File 'lib/turf/polygon_to_line.rb', line 17 def single_polygon_to_line(*args) raise NotImplementedError end |
#square(bbox) ⇒ Array<Float>
Takes a bounding box and calculates the minimum square bounding box that would contain the input.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/turf/square.rb', line 18 def square(bbox) west, south, east, north = bbox horizontal_distance = distance([west, south], [east, south]) vertical_distance = distance([west, south], [west, north]) if horizontal_distance >= vertical_distance vertical_midpoint = (south + north) / 2.0 [ west, vertical_midpoint - ((east - west) / 2.0), east, vertical_midpoint + ((east - west) / 2.0), ] else horizontal_midpoint = (west + east) / 2.0 [ horizontal_midpoint - ((north - south) / 2.0), south, horizontal_midpoint + ((north - south) / 2.0), north, ] end end |
#square_grid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/square_grid.rb', line 5 def square_grid(*args) raise NotImplementedError end |
#standard_deviational_ellipse(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/standard_deviational_ellipse.rb', line 5 def standard_deviational_ellipse(*args) raise NotImplementedError end |
#tag(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/tag.rb', line 5 def tag(*args) raise NotImplementedError end |
#tesselate(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/tesselate.rb', line 5 def tesselate(*args) raise NotImplementedError end |
#tin(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/tin.rb', line 5 def tin(*args) raise NotImplementedError end |
#to_mercator(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/projection.rb', line 5 def to_mercator(*args) raise NotImplementedError end |
#to_wgs84(*args) ⇒ Object
9 10 11 |
# File 'lib/turf/projection.rb', line 9 def to_wgs84(*args) raise NotImplementedError end |
#transform_rotate(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/transform_rotate.rb', line 5 def transform_rotate(*args) raise NotImplementedError end |
#transform_scale(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/transform_scale.rb', line 5 def transform_scale(*args) raise NotImplementedError end |
#transform_translate(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/transform_translate.rb', line 5 def transform_translate(*args) raise NotImplementedError end |
#triangle_grid(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/triangle_grid.rb', line 5 def triangle_grid(*args) raise NotImplementedError end |
#truncate(geojson, options = {}) ⇒ Object
Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry. (significant performance increase if true (note statement from js port. not checked in ruby)) var point = turf.point([
70.46923055566859,
58.11088890802906,
1508
]) var options = 3, coordinates: 2 var truncated = turf.truncate(point, options) //=truncated.geometry.coordinates => [70.469, 58.111]
//addToMap var addToMap = [truncated] /
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/turf/truncate.rb', line 29 def truncate(geojson, = {}) precision = [:precision] || 6 coordinates = [:coordinates] || 3 mutate = [:mutate] if !geojson raise("geojson is required") end if !mutate geojson = deep_dup(geojson) end # factor = Math.pow(10, precision) # geojson[:properties][:truncate] = "done" # Truncate Coordinates coord_each(geojson) do |coords| truncate_coords(coords, precision, coordinates) end geojson end |
#truncate_coords(coords, precision, coordinates) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/turf/truncate.rb', line 51 def truncate_coords(coords, precision, coordinates) # Remove extra coordinates (usually elevation coordinates and more) if coords.length > coordinates coords.slice!(coordinates..-1) end # coords.map do |coord| # coord.round(precision) # end # Truncate coordinate decimals (0...coords.length).each do |idx| coords[idx] = coords[idx].round(precision) end coords end |
#union(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/union.rb', line 5 def union(*args) raise NotImplementedError end |
#unkink_polygon(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/unkink_polygon.rb', line 5 def unkink_polygon(*args) raise NotImplementedError end |
#voronoi(*args) ⇒ Object
5 6 7 |
# File 'lib/turf/voronoi.rb', line 5 def voronoi(*args) raise NotImplementedError end |