npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

points2polygons

v0.0.10

Published

Batch point-in-polygon operations on GeoJSON files.

Downloads

7

Readme

Build Status points2polygons

Given a list of polygons and points, points2polygons will determine if each point is inside a polygon, using point-in-polygon.

If found, the point will be added to the polygon.

For example:

Given a list of polygons, polygons.geojson:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": { },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [0,0],
                        ...

and a list of points, points.csv:

address,        lat, lon
111 Point Lane, 1,   1

points2polygons will assign points to matching polygons, and generate a new GeoJSON file:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "points": [
                    {
                        "type": "Feature",
                        "properties": {
                            "address": "111 Point Lane"
                        }
                        "geometry": {
                            "type": "Point",
                            "coordinates": [1,1]
                        }
                    }
                ]
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [0,0],
                        ...

Points with no polygons will be placed in their own GeoJSON file, orphans.geojson.

Aggregation!

points2polygons can perform sum and count aggregations. Let me explain. Say you have a town.geojson:

{
    "type": "Feature",
    "properties": {
        "name": "Polygonville"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [0,0],

and houses.csv:

address,        color, value, latitude, longitude
111 Point Lane, red,   100,   1,        1
222 Point Lane, green, 200,   2,        2
333 Point Lane, red,   300,   3,        3

Running points2polygons --polygons town.geojson --points houses.csv will correctly place the houses in our town, and generate something like this:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Polygonville",
                "points": [
                    // a house
                    {
                        "type": "Feature",
                        "properties": {
                            "address": "111 Point Lane",
                            "color": "red",
                            "value": "100"
                        }
                        "geometry": {
                            "type": "Point",
                            "coordinates": [1,1]
                        }
                    },
                    // another house
                    {
                        "type": "Feature",
                        "properties": {
                            "address": "222 Point Lane",
                            "color": "green",
                            "value": "200"
                        }
                        "geometry": {
                            "type": "Point",
                            "coordinates": [2,2]
                        }
                    },
                    ...
                ]
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [0,0],

But what I really want is a count of red and green houses in my town. In other words, I want to

count

by color. Use the --count param. Running points2polygons --polygons town.geojson --points houses.csv --count color generates something like this:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Polygonville",
                "green": 1, // there is one green house in the town
                "red": 2 // there are two red houses in the town
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [0,0],

properties doesn't contain points anymore, only the aggregation result.

Pretty incredible! But what I really want is a total of house values, by color. In other words, I want to

sum

value, and group by color. Use the --groupBy and --sum params. Running points2polygons --polygons town.geojson --points houses.csv --groupBy color --sum value generates something like this:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Polygonville",
                "green": 200, // this town's green houses are worth a total of 200
                "red": 400 // this towns's red houses are worth a total of 400
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [0,0],

Pretty incredible!

Installation

npm install points2polygons

Using it as a console utility

➜ points2polygons
Batch point-in-polygon operations. Creates a GeoJSON file of polygons containing points.
Usage: points2polygons

Options:
  -y, --polygons   a GeoJSON file of polygons                        [required]
  -t, --points     a CSV file of points                              [required]
  -i, --latitude   latitude field                                    [default: "latitude"]
  -e, --longitude  longitude field                                   [default: "longitude"]
  -d, --delimiter  delimiter character                               [default: ","]
  -o, --output     a GeoJSON file of polygons containing points      [default: "output.json"]
  -c, --count      aggregate points and count - group by this field  [default: null]
  -g, --groupBy    aggregate points and sum - group by this field    [default: null]
  -s, --sum        aggregate points and sum - sum this field         [default: null]

Using it as a library

require('points2polygons')

.batch(polygons, points, showProgress, count, groupBy, sum)

  • polygons: (required) a GeoJSON object of polygons.
  • points: (required) a GeoJSON object of points.
  • showProgress: (optional) a callback that gets fired per point processed, and receives the current point index.
  • count: (optional) if provided, will aggregate points and count this field. See example.
  • groupBy: (optional) if provided, will aggregate points and sum, grouping by this field. See example.
  • sum: (optional) if provided, will aggregate points and sum, summing by this field. See example.

Returns an object with two properties:

  • polygons: same as input, but each polygon has a points property containing corresponding points.
  • orphans: a GeoJSON object containing points with no polygons.

See also