pythonpolygonspysal

Pysal doesn't recognize some polygon neighbors


I'm using the Pysal tool for analyzing several shapefiles (type polygon) that contain the results of segmentations done in QGIS.

I load the shapefile using

segshp = pysal.weights.Queen.from_shapefile(vect)

being vect the name of the corresponding shapefile.

The problem is that when I look in the neighbors dictionary, if a polygon shares less than 2 pixels it is not considered a neighbor. Let me show a bit better:

Using ArcGIS, I calculate the polygon neighbors (Analysis tools --> Proximity ---> Polygon neighbors), and for example, polygon 1227 has 4 neighbors as shown in the image.

Neighbors calculated with ArcGIS

But when loading in Python using Pysal, polygon 1227 has only 3 neighbors and the one missing is 3307, which only shares a 2 pixel border. Same happens with other polygons that share only 1 or 2 pixels. This "threshold" of 2 pixels is just what I have seen analyzing some polygons.

Neighbors calculated with Pysal

Is there anyway of changing that "threshold" in order to consider all neighbors? i've seen in the Pysal documentation and thought maybe it's related to the command

pysal.weights.user.min_threshold_dist_from_shapefile

or something similar, but honestly I got no idea.

I also tried with the pysal.weights.Rook... option, but it recognizes less neighbors and finds more islands.

Thanks a lot


Solution

  • Thanks @Charles.

    Indeed that seems to be the problem. Checking the vertices, although polygon 1227 and 3307 share a common border, they don't share any vertices. Once I edited them manually and corrected a small difference in the coordenates of the new vertices, the Pysal tool recognize them as neighbors. Checking the geometry of the shapefile does not generates any invalid geometries, neither in Arcgis or QGIS.

    I'll try to figure a way to create the "missing" vertices in these cases, so I can "fix" the geometry before the Pysal line (any ideas for this matter are welcome).

    Anyway, for now and respecting the original question the issue has been solved.