Coder Social home page Coder Social logo

wegiangb / geojson-js-utils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from max-mapper/geojson-js-utils

0.0 2.0 0.0 274 KB

JavaScript helper functions for manipulating GeoJSON

License: BSD 2-Clause "Simplified" License

JavaScript 100.00%

geojson-js-utils's Introduction

GeoJSON Utilities for JavaScript

Here you will find some functions to help you manipulate and work with GeoJSON objects.

Some algorithms adapted from bjwbell/canvas-geolib

How to use!

Load up in a browser OR npm install geojson-utils

var gju = require('geojson-utils');

You now have an object named gju that contains all of the helper functions.

Remember, GeoJSON coordinates are ordered [x,y] or [longitude,latitude] to comply with the Open Geospatial Consortium's recommendation!

Line intersections

gju.linesIntersect({ "type": "LineString", "coordinates": [[0, 2], [5, 2]] },
                 { "type": "LineString", "coordinates": [[3, 0], [3, 4], [4,4], [4,0]] })
// [{"type":"Point","coordinates":[2,3]},{"type":"Point","coordinates":[2,4]}]

gju.linesIntersect({ "type": "LineString", "coordinates": [[0, 2], [5, 2]] },
                 { "type": "LineString", "coordinates": [[0, 0], [5, 0]] })
// false

Point in polygon

gju.pointInPolygon({"type":"Point","coordinates":[3,3]},
                 {"type":"Polygon", "coordinates":[[[0,0],[6,0],[6,6],[0,6]]]})
// [{"type":"Point","coordinates":[3,3]}]
gju.pointInPolygon({"type":"Point","coordinates":[-1,-1]},
                 {"type":"Polygon", "coordinates":[[[0,0],[6,0],[6,6],[0,6]]]})
// false

Radius filtering

If you retrieve a bunch of results from a bounding box query (common with R-tree geo DBs), but you want to filter the rectangular result set by circular radius:

// get the center of the original bounding box
var center = gju.rectangleCentroid({
  "type": "Polygon",
  "coordinates": [[[-122.677, 45.523], [-122.675, 45.524]]]
}),
// radius (in meters)
radius = 100;

for (var i in geometryObjectsWithinBBox) {
  if (gju.geometryWithinRadius(geometryObjectsWithinBBox[i], center, radius)) {
    // ... do stuff with objects inside the circle
  }
}

Distance between two points

Uses the Haversine distance formula to calculate the distance between two points on the Earth's curved surface (as the crow flies, no hills!). Returns the distance in meters.

gju.pointDistance({type: 'Point', coordinates:[-122.67738461494446, 45.52319466622903]},
                  {type: 'Point', coordinates:[-122.67652630805969, 45.52319466622903]})
// 66.86677669313518

and much much more!

License

The MIT License

Copyright (c) 2010 Max Ogden

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

geojson-js-utils's People

Contributors

max-mapper avatar bonifaido avatar starefossen avatar abernier avatar abenrob avatar filmaj avatar ghxiao avatar jasonsanford avatar mapmeld avatar slava avatar

Watchers

James Cloos avatar Gordon avatar

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.