danioxoli / HotSpotAnalysis_Plugin

A QGIS plugin for hotspot analysis
GNU General Public License v3.0
56 stars 13 forks source link

Buffer problem? #91

Open Solivetti opened 1 year ago

Solivetti commented 1 year ago

Good morning, I have used the Hotspot plugin several times with satisfying results. Recently, I tried to use the Hotspot plugin within a QGIS project including many layers and I met with this warning, which pops up as soon as I click on the Hotspot icon, and therefore even before trying to fill the Hotspot window. The shapefile with grid & count is top of the list. The count field does not contain null cases. QGIS version is 3.28.11. RAM is 32 Giga. I found that the warning is associated with this specific project. Indeed, I started a new project with only the basic layers of the previous project, saved it and started again QGIS: the problem is over. I am asking myself however which is the cause of the trouble and whether is it possible to find a solution less “manual”. 2023-10-15T17:47:18 WARNING Traceback (most recent call last): File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\util.py", line 1028, in get_points_array data = np.vstack([np.array(shape.centroid) for shape in iterable]) File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\util.py", line 1028, in data = np.vstack([np.array(shape.centroid) for shape in iterable]) AttributeError: 'Point' object has no attribute 'centroid'

         During handling of the above exception, another exception occurred:

         Traceback (most recent call last):
          File "C:\Users/solivetti/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\HotspotAnalysis\hotspot_analysis.py", line 532, in 
          self.dlg.comboBox.currentIndexChanged.connect(lambda: self.load_comboBox())
          File "C:\Users/solivetti/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\HotspotAnalysis\hotspot_analysis.py", line 473, in load_comboBox
          thresh = pysal.lib.weights.user.min_threshold_dist_from_shapefile(path)
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\user.py", line 86, in min_threshold_dist_from_shapefile
          points = get_points_array_from_shapefile(shapefile)
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\util.py", line 1077, in get_points_array_from_shapefile
          data = get_points_array(f)
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\util.py", line 1030, in get_points_array
          data = np.vstack([shape for shape in iterable])
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\weights\util.py", line 1030, in 
          data = np.vstack([shape for shape in iterable])
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\fileio.py", line 254, in __next__
          r = self.__read()
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\fileio.py", line 315, in __read
          row = self._read()
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\iohandlers\pyShpIO.py", line 144, in _read
          rec = self.dataObj.get_shape(self.pos)
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\util\shapefile.py", line 382, in get_shape
          return self.shape.unpack(io.BytesIO(self.fileObj.read(byts)))
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\util\shapefile.py", line 631, in unpack
          return _unpackDict(cls.USTRUCT, dat)
          File "C:\Users\solivetti\AppData\Roaming\Python\Python39\site-packages\pysal\lib\io\util\shapefile.py", line 135, in _unpackDict
          items = unpack(struct['order'] + struct['fmt'],
         struct.error: unpack requires a buffer of 20 bytes

Thanks for your attention, Luigi M. Solivetti Sapienza University of Rome

danioxoli commented 1 year ago

Dear Luigi,

by passing through the first error log (AttributeError: 'Point' object has no attribute 'centroid') it seems your dataset has some geometry which is not a polygon. Can you try to run the plugin with this dataset which I am sure has polygon geometries which can be handled by the PySAL function in the object?

The second log may be due to bad formatting of the input file (https://docs.python.org/3/library/struct.html). Try to check/change the encoding of the shapefile or leave only the numerical field in the attribute table.

Best,

Daniele

Solivetti commented 1 year ago

Good evening, I have carried out some further experiments in order to understand the origin of the abovementioned errors. I discovered that the errors do not seem to depend on the geometry of the layer containing the count field or on any other feature of the said layer. Indeed, the errors would disappear by loading up the same layer into a different project. I have assumed that the problem was (and still is) in the original project. This project contains many layers. Therefore, the errors might be caused by the presence of several other layers, although I did not activate them when I tried to run Hotspot. Was it a problem of RAM? There are a few facts in conflict with this hypothesis: firstly, the amount of the PC’s RAM is substantial (32GB) and the PC was using ~26% of its memory at the time of the attempt; secondly, there was no mention of a problem of RAM; thirdly, as I said before, I did not activate the other layers. Ultimately, I have made little progress in my search for the origin of the errors. Anyway, I thank you very much for your attention. Luigi M. Solivetti

danioxoli commented 1 year ago

Dear Luigi,

I would exclude any problem with RAM. However, it is difficult to replicate the error locally. If you are willing to share (privately) the input shapefile which generates this error, I can try to run some tests on a clean QGIS/Python env. to i) understand if the problem arises from the layer listing function of the plugin that interacts with the layer panel or ii) if the problem is in connected to some features of the input shapefile using directly PySAL from the console.

Best, Daniele