Giter VIP home page Giter VIP logo

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

abenrob avatar abernier avatar bonifaido avatar filmaj avatar ghxiao avatar jasonsanford avatar mapmeld avatar max-mapper avatar slava avatar starefossen 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

geojson-js-utils's Issues

README: order of coordinates is [x,y]

Coordinates in GeoJSON are usually [longitude, latitude] or [x,y]. I think the README intended to point this out but currently says

GeoJSON coordinates are ordered [y,x]

Simplify method

hi!

great lib!

But I don't understood because the simplify method don't support geojson Linestring instead of a geojson array points, why? How can simplify a Geojson linestring? :o
Usually working with geojson data the polyline is defined as a LineString:

{
   "type": "LineString",
   "coordinates": [
       [12.72, 42.23], [12.34, 42.32], ....
   ]
}

gju.simplify = function (source, kink) { /* source[] array of geojson points */

maintainers wanted!

i'm not currently doing any geo projects so I havent touched this in a while. if anyone wants to help maintain this let me know here with your github + npm username and i'll add you as a collaborator

cc @zeke @pwiecz

pointInPolygon crashes on MultiPolygon with no positions

Including this valid but unusual feature in a FeatureCollection will lead to a crash in geojson-js-utils at https://github.com/maxogden/geojson-js-utils/blob/master/geojson-utils.js#L55 because the code assumes that rings will have positions, but that isn't guaranteed by the GeoJSON specification.

{
  "type": "Feature",
  "id": "4a1a777ddffb28ed6caabd67eabf49bef002bf82",
  "properties": {
    "fill": "#483D8B",
    "fill-opacity": 0.9,
    "stroke": "#000000",
    "stroke-width": 2,
    "stroke-opacity": 0.9,
    "headline": "Winter Weather Advisory issued November 20 at 5:18PM EST until November 21 at 4:00AM EST by NWS Pittsburgh"
  },
  "geometry": {
    "type": "MultiPolygon",
    "coordinates": [
      []
    ]
  }
}

Tests fail

Last test (points distance) fails:


/Users/imslavko/work/geojson-js-utils/test.js:38
Math.floor(gju.pointDistance(fairyLand, navalBase)) !== 5852) throw new Error(
                                                                    ^
Error
    at Object.<anonymous> (/Users/imslavko/work/geojson-js-utils/test.js:38:73)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

pointInPolygon does not work if polygon contains pole

I don't know if the project is developed any more, but anyway:
If polygon contains pole pointInPolygon does not work.

  1. founding bounding box does not work either
  2. even if we ignore bounding box check it still does not work (always returns false)

I think that polygons crossing 180th meridian may also not work.

Polygon not closed

In the example, the polygon is not closed (first point not repeated) however this is required for GeoJSON polygons. Is this a typo in the docs or doesn't the implementation handle standard GeoJSON polygons?

Centroid of a MultiPolygon

Hi,

Would it be possible to provide the centroid method catered to handle MultiPolygons as well.

It would be ideal to find the weighted centroid of the multipolygon, so things like labels can be rendered within the "natural" center of a complex polygon.

I have currently worked around the limitation by calling the centroid method as follows, but a correct implementation would be a nice to have.

var center = gju.centroid(shape.type === "MultiPolygon" ? { type: "Polygon", coordinates: shape.coordinates[0] } : shape);

Thanks,
Aaron

gju.destinationPoint coordinate are in incorrect order.

Line 365:
var lat1 = gju.numberToRadius(pt.coordinates[0]);
var lon1 = gju.numberToRadius(pt.coordinates[1]);

If I'm using "correct" GeoJson, lat is coordinates[1] and lng is coordinates[0], which when passed in to this gets an entirely wrong result.

Can you add an in-browser example to the README?

Can you explain how to use geojson-utils.js in a web browser client-side without node.js? Is this even possible? I'm stumped by the 'require' keyword at the beginning of the README.

Thanks.

Arguments validation

It'll be great if arguments of utils are validated, it will allow to have more meaningful errors e.g. instead of "Cannot read property '1' of undefined", util will crash with "Point object expected, received null"

@maxogden I will be happy to contribute such validation to the project (by introducing side ensurePoint etc. utils, and then making use of it in API).

Let me know what you think

geometryWithinRadius error when type is Point

There's an error with the geometryWithinRadius function. When the type is "Point", the pointDistance function expects the first parameter to be a coordinates array.

if (geometry.type == 'Point') {
  return gju.pointDistance(geometry, center) <= radius;
}

Should be changed to:

if (geometry.type == 'Point') {
  return gju.pointDistance(geometry.coordinates, center) <= radius;
}

Validating GeoJSON parser

Hi,

I haven't done any research if there is already one or not, but a validating parser for GeoJSON would be great. It would e.g. check if polygons are really closed.

Cheers,
Volker

Wierd issue with pointInPolygon

I'm getting crazy with a issue here, if the polygon is on south hemisphere the pointInPolygon returns false, if the point is inside a polygon on north hemisphere the function returns true.

What am I missing?

Intersection of 2 polygons?

As far as I can see there is no method for checking whether 2 polygons intersect.

Can this be an enhancement?

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.