Giter Club home page Giter Club logo

turf's Introduction

turf

GitHub Actions Status Version Badge Gitter chat Backers on Open Collective Sponsors on Open Collective Coverage Status

A modular geospatial engine written in JavaScript

turfjs.org


Turf is a JavaScript library for spatial analysis. It includes traditional spatial operations, helper functions for creating GeoJSON data, and data classification and statistics tools. Turf can be added to your website as a client-side plugin, or you can run Turf server-side with Node.js (see below).

Installation

In Node.js

# get all of turf
npm install @turf/turf

# or get individual packages
npm install @turf/helpers
npm install @turf/buffer

In browser

Download the minified file, and include it in a script tag. This will expose a global variable named turf.

<script src="turf.min.js" charset="utf-8"></script>

You can also include it directly from a CDN:

<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>

TypeScript

TypeScript definitions are packaged with each module. No DefinitelyTyped packages required.

Other languages

Ports of Turf.js are available in:

  • Java (Android, Java SE)
  • Swift (iOS, macOS, tvOS, watchOS, Linux)
    • Turf for Swift is experimental and its public API is subject to change. Please use with care.

  • Dart/Flutter (Dart Web, Dart Native; Flutter for iOS, Android, macOS, Windows, Linux, Web)
    • The Turf for Dart port is still in progress, the implementation status can be found in the README.


Data in Turf

Turf uses GeoJSON for all geographic data. Turf expects the data to be standard WGS84 longitude, latitude coordinates. Check out geojson.io for a tool to easily create this data.

NOTE: Turf expects data in (longitude, latitude) order per the GeoJSON standard.

Most Turf functions work with GeoJSON features. These are pieces of data that represent a collection of properties (ie: population, elevation, zipcode, etc.) along with a geometry. GeoJSON has several geometry types such as:

  • Point
  • LineString
  • Polygon

Turf provides a few geometry functions of its own. These are nothing more than simple (and optional) wrappers that output plain old GeoJSON. For example, these two methods of creating a point are functionally equivalent:

// Note order: longitude, latitude.
var point1 = turf.point([-73.988214, 40.749128]);

var point2 = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    // Note order: longitude, latitude.
    coordinates: [-73.988214, 40.749128]
  },
  properties: {}
};

Browser support

Turf packages are compiled to target ES2017. However, the browser version of @turf/turf is transpiled to also include support for IE11. If you are using these packages and need to target IE11, please transpile the following packages as part of your build:

@turf/*
robust-predicates
rbush
tinyqueue

Contributors

This project exists thanks to all the people who contribute. If you are interested in helping, check out the Contributing Guide.

Backers

Thank you to all our backers! ๐Ÿ™ [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

turf's People

Contributors

1ec5 avatar atdrago avatar ath0mas avatar aurelien-defossez avatar camilleanne avatar chelm avatar davidchouse avatar deniscarriere avatar dependabot[bot] avatar dpmcmlxxvi avatar greenkeeper[bot] avatar jameslmilner avatar kochizufan avatar lyzidiamond avatar mfedderly avatar miccferr avatar monkeywithacupcake avatar morganherlocker avatar muziejus avatar pizzabrandon avatar pwilczynski avatar rowanwins avatar smallsaucepan avatar stebogit avatar stevage avatar tmcw avatar twelch avatar vamshi29292 avatar woutervanheeswijk-tomtom avatar zhuang-hao-ming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

turf's Issues

simplify

Takes a collection of detailed polygons and smooths out the edges to a given tolerance. It will probably use a Ramer Douglas Peucker algorithm or something similar. This will probably require the use of topojson to maintain edge integrity of geographies.

g.simplify(polys, 2000, function(err, smoothed){
  console.log(smoothed)
})

bezier

takes a linestring and curves it.

topo

encodes geojson as topology using topojson.

bisect

Takes a line and bisects it, returning the midpoint.

beizer

Beizer curve based smoothing for polygons and linestrings. I plan to use topojson as an intermediate format in conjunction with mapshaper's repair functionality to maintain topological integrity.

contour

Takes a set of points and creates contours from a property. The algorithm is something like the following:

  1. create tin from points with g.tin
  2. get the extent of the tin with g.extent
  3. create a grid within the extent using g.grid
  4. get the value at each point from the tin using g.tinpoint
  5. calculate the contour break point quantiles using g.quantile
  6. reclass the points using g.reclass
  7. pass the grid with data to the conrec algorithm
g.contours(points, 'elevation', function(err, contourPolygons){
  console.log(contourPolygons)
})

explode

Takes a feature or FeatureCollection and outputs all of the vertices as a collection of points.

cluster

Point clustering for rendering lots of points

sum

This should take a set of polygons, a set of points, and a field name. It will then add a [fieldName]+'_sum' which will be equal to the sum of the field for all points within the polygon.

interval

Classification based on equal intervals.

jenks

Jenks natural breaks classifier

union

Takes an featurecollection of polygons and outputs a unionized version, where overlapping polygons are merged.

g.union(polys, function(err, merged){
  // assuming polys contained two overlapping polys, a single polygon would be returned
  console.log(merged)
})

average

Takes a set of polygons, a set of points, and a field name, then outputs a polygon with a [fieldName]+'_avg' property appended that is the average of the points contained in the polygon.

area

Returns the area of a polygon in a specified unit.

g.area(poly, 'miles', function(err, area){
  console.log(area)
})

remove

Removes features that have a property equal to the input.

var points = g.featurecollection([g.point(1,1, {type: 'oak'}), 
                                          g.point(1,2, {type: 'maple'}), 
                                          g.point(2,1, {type: 'cedar'})])
g.remove(points, 'type', 'maple', function(err, remainingPts){
  // the maple point has been removed
  console.log(remainingPts) 
})

bboxPolygon

takes a bbox [xmin,ymin,xmax,ymax] and returns a rectangular polygon.

tag

Tag a point with a property from the polygon. An example would be adding a zipcode column to a point based on which zip polygon it falls in.

g.tag(points, polys, 'zipcode', function(err){

})

clipping

This could be very handy and should just considered a suggestion.

g.clip(polys, clippolys, function(err, clipedpolys){
console.log(clipedpolys)
})

flip

function that will switch the x, y of every coordinate in a feature or feature collection

quantile

Gets the percentile breaks for N equal breaks. Specify the property and the number of quantiles.

g.quantile(points, 'elevation', 5, function(err, quantiles){
  console.log(quantiles)
  // [0, 359.983, 689.226, 1023.864, 3984.234, 5113.349]
})

intersect

Gets a polygon representing the intersection between two other polygons.

g.intersect(poly1, poly2, function(err, result){
  console.log(result)
})

tinpoint

Interpolate an arbitrary point's z value from a tin.

g.tinpoint(point, tin, function(err, val){
  console.log(val)
})

sample

take a point collection and an integer and return a random sample of points

envelope

get the extents, then convert the bbox result to a polygon in a single step.

Demo site

Demo site featuring inputs and outputs displayed on leaflet maps.

size

changes the size of a bbox by the factor of x

Corrections to the examples in https://github.com/morganherlocker/turf

In the load example the arguments should be switched fron
t.load(geojsonFile, function(trees, err)
into

t.load(geojsonFile, function(err, trees)

In "distance" using degree: the degree is the same
on
t.distance(point1, point2, unit, function(err, distance)
and
t.distance(point2, point1, unit, function(err, distance)

is that correct?

The planepoint example does not work.

save

Save out a feature or collection to various output file types. This will start out with geojson, but I will probably add topojson, and shapefile as well. I do not want to make ogr2ogr a dependency here, so I will keep it pretty simple.

centroid

Returns a point that is the average of all points in a polygon.

g.centroid(point, poly, function(err, pt){
  console.log(pt)
})

erase

Takes 2 polygons. The 1st polygon is returned, but with any overlaps removed.

g.erase(poly1, poly2, function(err, poly3){
  // poly3 is now poly1 minus any portion that overlapped with poly2
  console.log(poly3)
})

reclass

Change values that fall within a range to a discrete value.

var ranges = [[0, 50, 1], [50, 100, 2], [100, Infinity, 3]]
g.reclass(points, 'trees', ranges, function(err, reclassPts){
  console.log(reclassPts)
})

square

get the square extents of a feature or featurcollection of any type.

inside

Return true if point is inside of a polygon. This will probably use the even odd rule algorithm.

g.inside(point(10,5), polygon, function(err, poly){
  console.log(poly)
})

aggregate

aggregate takes a set of polys, a set of points, and an aggregation field map, then automatically aggs all of the point fields over to the polygons.

A field map would look something like this:

[
{
in: 'population',
out: 'pop_avg',
aggregate: 'average'
}, ...
]

style

I have no idea if this is even possible, but I would like to be able to include some sort of styling info with the features. It is my understanding that each platform has its own way of styling and symbology, so this may not be possible. Research this issue and come to a conclusion one way or the other.

filter

Filter out features based on a property

g.filter(polys, 'ZipCode', '29407', function(err, matchingPoly){
  console.log(matchingPoly)
})

fix casing consistency

featurecollection, planepoint, and a couple others need to be consistent with the rest of the api for casing.

featurecollection

Takes an array of features and sets up a basic featurecollection.

var fc = g.featurecollection([g.point(1,2), g.point(19, 23), g.point(11, 13), g.point(-3, -14)])
console.log(fc)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.