Тест на содержание полигона в matplotlib artist

У меня есть следующий код, собранный изначально изВот., который использует matplotlib, shapely, cartopy, чтобы нарисовать карту мира.

Когда клик сделан, мне нужно определить, в какой стране он был сделан. Я могу добавитьpick_event обратный вызов к холсту, однако, вызывается у каждого художника. (cartopy.mpl.feature_artist.FeatureArtist, который соответствует стране).

Как определить локализацию для художника и события мыши с координатами x, y?

я пробовалartist.get_clip_box().contains, но на самом деле это не многоугольник, а простой прямоугольник.

Тест сдерживания по умолчанию дляFeatureAristс этоNone, поэтому я должен был добавить свой собственный тест сдерживания.

Как я могу правильно проверить наличие точки события мыши внутри FeatureArtist?

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
import itertools, pdb, subprocess, time, traceback
from itertools import *
import numpy as np
from pydoc import help as h

shapename = 'admin_0_countries'
countries_shp = shpreader.natural_earth(resolution='110m',
                                        category='cultural', name=shapename)

earth_colors = np.array([(199, 233, 192),
                                (161, 217, 155),
                                (116, 196, 118),
                                (65, 171, 93),
                                (35, 139, 69),
                                ]) / 255.
earth_colors = itertools.cycle(earth_colors)

ax = plt.axes(projection=ccrs.PlateCarree())


def contains_test ( artist, ev ):
    print "contain test called"
    #this containmeint test is always true, because it is a large rectangle, not a polygon
    #how to define correct containment test
    print "click contained in %s?: %s" % (artist.countryname, artist.get_clip_box().contains(ev.x, ev.y))
    return True, {}

for country in shpreader.Reader(countries_shp).records():
    # print country.attributes['name_long'], earth_colors.next()
    art = ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                      facecolor=earth_colors.next(),
                      label=country.attributes['name_long'])

    art.countryname = country.attributes["name_long"] 
    art.set_picker(True)
    art.set_contains(contains_test)
    def pickit ( ev ):
        print "pickit called"
        print ev.artist.countryname



def onpick ( event ):
    print "pick event fired"

ax.figure.canvas.mpl_connect("pick_event", onpick)


def onclick(event):
    print 'button=%s, x=%s, y=%s, xdata=%s, ydata=%s'%(event.button, event.x, event.y, event.xdata, event.ydata)

ax.figure.canvas.mpl_connect('button_press_event', onclick)
plt.show()

Ответы на вопрос(1)

Ваш ответ на вопрос