Code Monkey home page Code Monkey logo

Comments (5)

ccev avatar ccev commented on August 22, 2024 1

v2 uses polylabel to correctly put markers inside polygons.

from nestwatcher.

clburlison avatar clburlison commented on August 22, 2024

I dug into this a little bit and currently this project is already using a library that utilizes that formula ( https://github.com/Toblerity/Shapely). While it isn't calling the polylabel function directly it is indirectly getting the same polygon center point.

Tested with a few different values and really couldn't come up with a better solution than what is currently already implemented. It's possible two completely separate python developers implemented the initial project incorrectly...but it's much more likely mapbox's 2016 claim of "...guaranteeing the correct result in all cases." is false.

As it stands I have over 300 nests generated via this project and less than 15 of them have center points outside of the nest polygon. I stopped digging when I saw both libraries produced roughly the same results but sample code included below if anyone wants to look some more.

#pip install python-polylabel
#pip install shapely
#pip install geojson

from geojson import (
    Feature,
    FeatureCollection,
    dumps
)

from polylabel import polylabel
from shapely.ops import polylabel as polylabel2
from shapely.geometry import shape
import json

NEST_NAMES = ['Foobar Park', 'Foobar2 Park', 'Foobar3 Park', 'Foobar4']
NEST_IDS = [605XXXX44]

with open("nests.json") as json_file:
    json_data = json.load(json_file)

for item in json_data['features']:
    if item['properties']['name'] in NEST_NAMES:
        print(item['properties']['name'])
        print(polylabel(item['geometry']['coordinates']))
        print(polylabel2(shape(item['geometry']), tolerance=10))
    if item['id'] in NEST_IDS:
        print(polylabel(item['geometry']['coordinates']))

from nestwatcher.

Mygod avatar Mygod commented on August 22, 2024

Have you set the precision properly? The default precision is 1.0 which is probably not what you want.

from nestwatcher.

Mygod avatar Mygod commented on August 22, 2024
from geojson import (
    Feature,
    FeatureCollection,
    dumps
)

from polylabel import polylabel
from shapely.ops import polylabel as polylabel2
from shapely.geometry import shape
import json

with open("nests.json") as json_file:
    json_data = json.load(json_file)

for item in json_data['features']:
    print(item['properties']['name'])
    print(polylabel(item['geometry']['coordinates'], 1e-6))
    point = polylabel2(shape(item['geometry']), tolerance=1e-6)
    print(point)
    assert shape(item['geometry']).contains(point)
    print()

Changed precision/tolerance to 1e-6 (~0.1m). All the points are inside the polygon.

from nestwatcher.

Mygod avatar Mygod commented on August 22, 2024

Was this fixed?

from nestwatcher.

Related Issues (20)

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.