Repository: rasterio/rasterio

Issue:
1.2b2 from wheel can't locate proj.db
The PROJ search path isn't set properly. I had thought that the tests run by the wheel builder would find such bugs, but no.

```
$ rio info ~/projects/rasterio/tests/data/RGB.byte.tif 
Traceback (most recent call last):
  File "rasterio/_base.pyx", line 1455, in rasterio._base._osr_from_crs
  File "rasterio/_err.pyx", line 192, in rasterio._err.exc_wrap_int
rasterio._err.CPLE_AppDefinedError: PROJ: proj_create_from_database: cannot build projectedCRS 32618: SQLite error on SELECT extent.description, extent.south_lat, extent.north_lat, extent.west_lon, extent.east_lon, scope.scope, (CASE WHEN scope.scope LIKE '%large scale%' THEN 0 ELSE 1 END) AS score FROM usage JOIN extent ON usage.extent_auth_name = extent.auth_name AND usage.extent_code = extent.code JOIN scope ON usage.scope_auth_name = scope.auth_name AND usage.scope_code = scope.code WHERE object_table_name = ? AND object_auth_name = ? AND object_code = ? ORDER BY score, usage.auth_name, usage.code: no such table: usage

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sean/.local/bin/rio", line 8, in <module>
    sys.exit(main_group())
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/sean/.local/pipx/venvs/rasterio/lib/python3.7/site-packages/rasterio/rio/info.py", line 90, in info
    info['lnglat'] = src.lnglat()
  File "rasterio/_base.pyx", line 986, in rasterio._base.DatasetBase.lnglat
  File "rasterio/_base.pyx", line 1394, in rasterio._base._transform
  File "rasterio/_base.pyx", line 1461, in rasterio._base._osr_from_crs
rasterio.errors.CRSError: PROJ: proj_create_from_database: cannot build projectedCRS 32618: SQLite error on SELECT extent.description, extent.south_lat, extent.north_lat, extent.west_lon, extent.east_lon, scope.scope, (CASE WHEN scope.scope LIKE '%large scale%' THEN 0 ELSE 1 END) AS score FROM usage JOIN extent ON usage.extent_auth_name = extent.auth_name AND usage.extent_code = extent.code JOIN scope ON usage.scope_auth_name = scope.auth_name AND usage.scope_code = scope.code WHERE object_table_name = ? AND object_auth_name = ? AND object_code = ? ORDER BY score, usage.auth_name, usage.code: no such table: usage
```


Discussion:
This looks like the culprit: https://github.com/mapbox/rasterio/blob/master/rasterio/_env.pyx#L372. Older, incompatible PROJ data previously installed on my system is being found and the data in the module is ignored.
@sgillies I posted the same in https://rasterio.groups.io/g/main/message/673
@snowman2 replied that it might be linked to https://pyproj4.github.io/pyproj/stable/gotchas.html#internal-proj-error-sqlite-error-on-select but I haven't tried 
Here is the search logic used by pyproj in case it is helpful: [docs](https://pyproj4.github.io/pyproj/stable/api/datadir.html#data-directory) | [code](https://github.com/pyproj4/pyproj/blob/552edb3613e471837e13143fb46fba6ff92621f6/pyproj/datadir.py#L52)

```
1. The one set by pyproj.datadir.set_data_dir (if exists & valid)
2. The internal proj directory (if exists & valid)
3. The directory in PROJ_LIB (if exists & valid)
4. The directory on sys.prefix (if exists & valid)
5. The directory on the PATH (if exists & valid)
```


Resolving patch:
diff --git a/rasterio/env.py b/rasterio/env.py
--- a/rasterio/env.py
+++ b/rasterio/env.py
@@ -639,6 +639,11 @@ def wrapper(*args, **kwds):
     path = os.environ["PROJ_LIB"]
     set_proj_data_search_path(path)
 
+elif PROJDataFinder().search_wheel():
+    path = PROJDataFinder().search_wheel()
+    log.debug("PROJ data found in wheel, setting to %r.", path)
+    set_proj_data_search_path(path)
+
 # See https://github.com/mapbox/rasterio/issues/1631.
 elif PROJDataFinder().has_data():
     log.debug("PROJ data files are available at built-in paths")


Test patch: