makinacorpus / django-geojson

django-geojson is a collection of helpers to (de)serialize (Geo)Django objects into GeoJSON.
GNU Lesser General Public License v3.0
258 stars 70 forks source link

OSError exception #125

Open andywar65 opened 3 months ago

andywar65 commented 3 months ago

django-geojson tries to use django.contrib.gis objects, but if ImportError or ImproperlyConfigured exceptions are raised, it fallbacks to custom nogeos objects. In my case (working on Windows) an OSError exception is raised, breaking the code. I added this exception to a fork of the library and everything seems to work fine:

# djgeojson/templatetags/geojson_tags.py
except (ImportError, ImproperlyConfigured, OSError):

# djgeojson/fields.py
except (ImportError, OSError):

# djgeojson/serializers.py
except (ImportError, ImproperlyConfigured, OSError):

Of course my issue depends on a local configuration, but the fix suggested makes life easier to the unexperienced. Thanks a lot for the library, by the way!

Gagaro commented 3 months ago

I'm not sure I understand, why did you have the OSError in the first place?

andywar65 commented 3 months ago

I'm on Windows 10 Pro, build 19045.4474 (not WSL) Let's say I create a virtual environment and activate it, and pip install django and django-geojson. Then I start a django project and add 'djgeocad' to INSTALLED_APPS. I migrate and run the server, that crashes. This is the error I get:

Exception in thread django-main-thread:
Traceback (most recent call last):
[...]
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\djgeojson\templatetags\geojson_tags.py", line 8, in <module>
    from django.contrib.gis.db.models.fields import GeometryField
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
    import django.contrib.gis.db.models.functions  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\functions.py", line 3, in <module>
    from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
    from django.contrib.gis.gdal import GDALException
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\__init__.py", line 29, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\datasource.py", line 41, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
    from django.contrib.gis.gdal.prototypes import ds as vcapi
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 10, in <module>
    from django.contrib.gis.gdal.libgdal import lgdal
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 69, in <module>
    lgdal = CDLL(lib_path)
            ^^^^^^^^^^^^^^
  File "C:\Users\Andrea\AppData\Local\Programs\Python\Python312\Lib\ctypes\__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 127] The specified procedure could not be found

It is probably a configuration error. If you go to djgeojson/templatetags/geojson_tags.py from line 7 to 12 you find this:

try:
    from django.contrib.gis.db.models.fields import GeometryField
    from django.contrib.gis.geos import GEOSGeometry
except (ImportError, ImproperlyConfigured):
    from ..nogeos import GEOSGeometry
    from ..fields import GeometryField

If you add the OSError to the exceptions (same for the other two files in the opening message of the issue), everything works fine. Thank you for your help