{"action":"created","issue":{"url":"https://api.github.com/repos/mapbox/rasterio/issues/888","repository_url":"https://api.github.com/repos/mapbox/rasterio","labels_url":"https://api.github.com/repos/mapbox/rasterio/issues/888/labels{/name}","comments_url":"https://api.github.com/repos/mapbox/rasterio/issues/888/comments","events_url":"https://api.github.com/repos/mapbox/rasterio/issues/888/events","html_url":"https://github.com/mapbox/rasterio/issues/888","id":178296724,"number":888,"title":"Importing rasterio leads to OGR Failure in GeoDjango","user":{"login":"lordi","id":232086,"avatar_url":"https://avatars.githubusercontent.com/u/232086?v=3","gravatar_id":"","url":"https://api.github.com/users/lordi","html_url":"https://github.com/lordi","followers_url":"https://api.github.com/users/lordi/followers","following_url":"https://api.github.com/users/lordi/following{/other_user}","gists_url":"https://api.github.com/users/lordi/gists{/gist_id}","starred_url":"https://api.github.com/users/lordi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lordi/subscriptions","organizations_url":"https://api.github.com/users/lordi/orgs","repos_url":"https://api.github.com/users/lordi/repos","events_url":"https://api.github.com/users/lordi/events{/privacy}","received_events_url":"https://api.github.com/users/lordi/received_events","type":"User","site_admin":false},"labels":[{"id":64167168,"url":"https://api.github.com/repos/mapbox/rasterio/labels/bug","name":"bug","color":"fc2929","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/mapbox/rasterio/milestones/8","html_url":"https://github.com/mapbox/rasterio/milestone/8","labels_url":"https://api.github.com/repos/mapbox/rasterio/milestones/8/labels","id":686337,"number":8,"title":"1.0","description":null,"creator":{"login":"sgillies","id":33697,"avatar_url":"https://avatars.githubusercontent.com/u/33697?v=3","gravatar_id":"","url":"https://api.github.com/users/sgillies","html_url":"https://github.com/sgillies","followers_url":"https://api.github.com/users/sgillies/followers","following_url":"https://api.github.com/users/sgillies/following{/other_user}","gists_url":"https://api.github.com/users/sgillies/gists{/gist_id}","starred_url":"https://api.github.com/users/sgillies/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgillies/subscriptions","organizations_url":"https://api.github.com/users/sgillies/orgs","repos_url":"https://api.github.com/users/sgillies/repos","events_url":"https://api.github.com/users/sgillies/events{/privacy}","received_events_url":"https://api.github.com/users/sgillies/received_events","type":"User","site_admin":false},"open_issues":16,"closed_issues":135,"state":"open","created_at":"2014-06-10T00:10:13Z","updated_at":"2017-02-04T09:01:02Z","due_on":null,"closed_at":null},"comments":6,"created_at":"2016-09-21T09:42:55Z","updated_at":"2017-02-09T05:49:03Z","closed_at":null,"body":"After importing, calling \"transform\" on a GEOS Point leads to an exception.\n- rasterio 0.36\n- GDAL 1.10.1\n- Python 3.4.2\n\n```\nPython 3.4.2 (default, Oct  8 2014, 10:45:20) \n[GCC 4.9.1] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> from django.contrib.gis.geos import Point\n>>> print(Point(2500000, 8500000, 0.0, srid=25832).transform('EPSG:4326', clone=True))\nSRID=4326;POINT Z (27.4517640749582235 70.8806800968583275 0.0000000000000000)\n>>> import rasterio\n>>> print(Point(2500000, 8500000, 0.0, srid=25832).transform('EPSG:4326', clone=True))\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/geos/geometry.py\", line 505, in transform\n    g.transform(ct)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/geometries.py\", line 393, in transform\n    capi.geom_transform_to(self.ptr, sr.ptr)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/prototypes/errcheck.py\", line 119, in check_errcode\n    check_err(result, cpl=cpl)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/error.py\", line 72, in check_err\n    raise e(msg)\ndjango.contrib.gis.gdal.error.GDALException: OGR failure.\n```\n\nI compared the environment after importing rasterio, and saw that it added \"PROJ_LIB\" which pointed to non-existing directory `(...)lib/python3.4/site-packages/rasterio/proj_data`. But removing this key has no effect so I figure the error is caused by something else:\n\n```\n>>> import os\n>>> del os.environ['PROJ_LIB']\n>>> print(Point(2500000, 8500000, 0.0, srid=25832).transform('EPSG:4326', clone=True))\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/geos/geometry.py\", line 505, in transform\n    g.transform(ct)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/geometries.py\", line 393, in transform\n    capi.geom_transform_to(self.ptr, sr.ptr)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/prototypes/errcheck.py\", line 119, in check_errcode\n    check_err(result, cpl=cpl)\n  File \"/var/www/visweb/maps/lib/python3.4/site-packages/django/contrib/gis/gdal/error.py\", line 72, in check_err\n    raise e(msg)\ndjango.contrib.gis.gdal.error.GDALException: OGR failure.\n```\n\nAny hints?\n"},"comment":{"url":"https://api.github.com/repos/mapbox/rasterio/issues/comments/278553585","html_url":"https://github.com/mapbox/rasterio/issues/888#issuecomment-278553585","issue_url":"https://api.github.com/repos/mapbox/rasterio/issues/888","id":278553585,"user":{"login":"geowurster","id":2676083,"avatar_url":"https://avatars.githubusercontent.com/u/2676083?v=3","gravatar_id":"","url":"https://api.github.com/users/geowurster","html_url":"https://github.com/geowurster","followers_url":"https://api.github.com/users/geowurster/followers","following_url":"https://api.github.com/users/geowurster/following{/other_user}","gists_url":"https://api.github.com/users/geowurster/gists{/gist_id}","starred_url":"https://api.github.com/users/geowurster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geowurster/subscriptions","organizations_url":"https://api.github.com/users/geowurster/orgs","repos_url":"https://api.github.com/users/geowurster/repos","events_url":"https://api.github.com/users/geowurster/events{/privacy}","received_events_url":"https://api.github.com/users/geowurster/received_events","type":"User","site_admin":false},"created_at":"2017-02-09T05:49:03Z","updated_at":"2017-02-09T05:49:03Z","body":"@fxthomas @perrygeo @sgillies @lordi This bit me indirectly recently as well, although it involved using the OSGEO bindings rather than GeoDjango.  There are a couple things going on.\r\n\r\nI believe this is ultimately a byproduct of #968, which has been fixed in PR #969.  The problem is that in some cases `rasterio.Env()` does not properly tear down and quietly leaves some options in the GDAL environment, specifically `CHECK_WITH_INVERT_PROJ=ON`.  Just importing `0.36.0` causes the error because it immediately modifies the GDAL environment rather than when the `rasterio.Env()` context manager starts.  I can also recreate the error on Linux, but not on Mac OSX.\r\n\r\nSo, there are 3 Rasterio states affecting this ticket, each with slightly different behavior:\r\n\r\n1. `0.36.0` - Importing Rasterio unintentionally starts a `rasterio._env.GDALEnv()`.  `rasterio.Env()` does not tear down properly.\r\n2. `1.0a6 | master` - The import error is fixed, but `rasterio.Env()` does not tear down properly.\r\n3. PR #969 - The above issues are fixed.\r\n\r\nI haven't been able to distill the underlying problem to `osgeo.ogr` code, but this demonstrates the fundamental environment issue:\r\n\r\n```python\r\n# Django 1.10.5\r\nfrom django.contrib.gis.geos import Point\r\nfrom osgeo import gdal\r\ngdal.SetConfigOption('CHECK_WITH_INVERT_PROJ', 'ON')\r\nprint(Point(2500000, 8500000, 0.0, srid=25832).transform('EPSG:4326', clone=True))\r\n```\r\n\r\nand this demonstrates the fix in PR #969:\r\n\r\n```python\r\n# pip install git+git://github.com/mapbox/rasterio.git@teardown-env\r\nfrom django.contrib.gis.geos import Point\r\nfrom osgeo import gdal\r\nimport rasterio\r\nwith rasterio.Env():\r\n    pass\r\nprint(gdal.GetConfigOption('CHECK_WITH_INVERT_PROJ'))\r\nprint(Point(2500000, 8500000, 0.0, srid=25832).transform('EPSG:4326', clone=True))\r\n```\r\n\r\nSo, I think this justifies patching `0.36.0` with PR #969 and whatever commit prevents Rasterio from starting `GDALEnv()` on import.  It would also be very useful if someone more familiar with how GeoDjango builds geometries could figure out a simpler test case using `osgeo.ogr` objects.  I suspect Fiona's `fiona.transform.transform_geom()` could have a similar failure mode and would like to test."}}
{"actor":{"display_login":"geowurster"}}