GDAL on Windows

From Wildsong
Jump to navigationJump to search

I am so fed up with Windows. There is no good, smooth option. There are several partial, clunky options.

First there is ArcGIS Pro, which has Python bindings (outdated) and no gdal cli executable tools. Also, it ships with an obsolete version of Conda.

There is the Docker approach, but it won't let me mount my fast SSD which is at F:/ Docker gives me 100% access to all the tools but not my data.

There is OSGeo4W which has no Bash and that feels totally crippled to me.

OSGeo4W

Wants to be in C:\OSGeo4W but I put it in my home directory under Portable, probably my first mistake. I installed only the gdal tools. Now can I run them? Maybe if I set my path correctly? Ha ha so much optimism.

Docker

1. Install Docker Desktop for Windows and tell it you want to use Linux containers. (You don't gain much by using Windows containers, since Windows is already the problem we're trying to work around.)

2. Start it (there from the desktop icon). It will show up in the tray. It should now show up in Bash too. Try 'docker version' and glory in it.

3. Pull the very best GDAL from the hub.docker.com and run it,

docker pull osgeo/gdal
winpty docker run -it --rm osgeo/gdal bash
root@f291721f9302:/# gdalbuildvrt --version
GDAL 3.2.0dev-ae500ba309be9fcef9d589a47edfcaca364d1745, released 2020/07/24

Awesome, it works. To mount a volume so you can access local data, I had to run in PowerShell (not Bash, not Powershell ISE) for example

PS docker run -it --rm -v C:\Users\bwilson\source\repos\LiDAR\:/mnt osgeo/gdal bash
# cd /mnt
# ls
shows the right folder here, yahoo!
# gdalinfo *

ArcGIS Pro

2020-Jul-28 AGP version 2.6

AGP comes with conda. It comes with gdal 2.x which is currently outdated.

Find the newest version.

conda search -c conda-forge gdal

Install a new environment.

conda create --name=gdal3 -c conda-forge gdal=3.0.4

Activate it, if you are a normal Windows user

activate gdal3

Activate if you are in a bash shell

$ source /c/Users/bwilson/AppData/Local/ESRI/conda/envs/gdal3/Lib/venv/scripts/common/activate
$ which python
/c/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python

Try it

python
import osgeo.gdal


Build your own

ArcGIS 10 + Python + GDAL using OSGeo4W

OLD OLD OLD

2010-June-something

First install the installer. Get the Windows version for Python 2.6 and run it. See http://pypi.python.org/pypi/setuptools Add the C:\Python26\ArcGIS10.0\Scripts directory to your PATH. That allows your command shell to find and run easy_install.

Next install OSGeo4W for this. See http://trac.osgeo.org/osgeo4w/ Along with GDAL and PROJ4 which is what we're looking for right now, this installs lots of cool stuff like QGIS and uDig. (You can uncheck boxes if you are in a hurry or don't want the whole thing.)

You have to have gdal 1.7 installed before you install Python gdal. Turns out that's part of the OSGeo4W installation done in the previous section, so now all I have to do is figure out how to reference those files.

I can't figure that out yet.

Next step WOULD be to go install the Python packages using the installer. The easy_install GDAL" fails because it can't find the cpl_ports.h file that's installed with gdal in C:\OSGeo4W\

--

I got a little further along, I was able to get it to build and install and now it can't find _gdal.py I think possibly either it's not loading gdal17.dll or that's wrong and it really needs gdal171.dll

I downloaded source for gdal 171 (GDAL-1.7.1.tar.gz) and edited the file setup.cfg, adding the last two lines so it could find where OSGeo4W put things.

[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0

[build_ext] gdal_config = ../../apps/gdal-config include_dirs = C:/OSGeo4W/include library_dirs = C:/OSGeo4W/lib

import osgeo.gdal gives

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import osgeo.gdal

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>
 File "osgeo\__init__.py", line 21, in <module>
   _gdal = swig_import_helper()
 File "osgeo\__init__.py", line 13, in swig_import_helper
   import _gdal

ImportError: No module named _gdal

--- GOT IT (I've been making some notes here: http://wildsong.biz/index.php?title=Geoprocessing#Open_source)

Trying the Pypi

See http://pypi.python.org/pypi/GDAL/

Add "C:\Python26\ArcGIS10.0;C:\Python26\ArcGIS10.0\Scripts" to your PATH. Make sure it's ahead of OSGeo4W so that it does not find Python2.5 instead.

You have to have gdal 1.7 installed before you install Python gdal. Turns out that's part of the OSGeo4W installation done in the previous section, so now all I have to do is figure out how to reference those files.

I could not use "easy_install". I had to run build and install steps so that the compiler could find the right include and lib files.

I downloaded and unpacked the source file from the PyPi link above. Then I edited the config file! Here is mine:

[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0

[build_ext]
gdal_config = ../../apps/gdal-config
include_dirs = C:/OSGeo4W/include
library_dirs = C:/OSGeo4W/lib

I added the last two lines, to tell it where I put the OSGeo4W files.

After that I was able to type this

python setup.py build
python setup.py install

and it WORKED. Then I was able to do this:

python
import osgeo.osr










To get the import to happen I had to set the environment, and there is a script to help me do that in c:\OSGeo4W\bin called gdal17.bat

In a cmd window I run that then I start python....

C:\OSGeo4W\bin>python

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import osgeo.osr >>> dir(osgeo.osr) ['CoordinateTransformation', 'CoordinateTransformation_swigregister', 'DontUseEx ceptions', 'GetProjectionMethods', 'GetUserInputAsWKT', 'GetWellKnownGeogCSAsWKT ', 'SRS_DN_NAD27', 'SRS_DN_NAD83', 'SRS_DN_WGS72', 'SRS_DN_WGS84', 'SRS_PM_GREEN WICH', 'SRS_PP_AZIMUTH', 'SRS_PP_CENTRAL_MERIDIAN', 'SRS_PP_FALSE_EASTING', 'SRS _PP_FALSE_NORTHING', 'SRS_PP_FIPSZONE', 'SRS_PP_LANDSAT_NUMBER', 'SRS_PP_LATITUD E_OF_1ST_POINT', 'SRS_PP_LATITUDE_OF_2ND_POINT', 'SRS_PP_LATITUDE_OF_CENTER', 'S RS_PP_LATITUDE_OF_ORIGIN', 'SRS_PP_LATITUDE_OF_POINT_1', 'SRS_PP_LATITUDE_OF_POI NT_2', 'SRS_PP_LATITUDE_OF_POINT_3', 'SRS_PP_LONGITUDE_OF_1ST_POINT', 'SRS_PP_LO NGITUDE_OF_2ND_POINT', 'SRS_PP_LONGITUDE_OF_CENTER', 'SRS_PP_LONGITUDE_OF_ORIGIN ', 'SRS_PP_LONGITUDE_OF_POINT_1', 'SRS_PP_LONGITUDE_OF_POINT_2', 'SRS_PP_LONGITU DE_OF_POINT_3', 'SRS_PP_PATH_NUMBER', 'SRS_PP_PERSPECTIVE_POINT_HEIGHT', 'SRS_PP _PSEUDO_STD_PARALLEL_1', 'SRS_PP_RECTIFIED_GRID_ANGLE', 'SRS_PP_SATELLITE_HEIGHT ', 'SRS_PP_SCALE_FACTOR', 'SRS_PP_STANDARD_PARALLEL_1', 'SRS_PP_STANDARD_PARALLE L_2', 'SRS_PP_ZONE', 'SRS_PT_ALBERS_CONIC_EQUAL_AREA', 'SRS_PT_AZIMUTHAL_EQUIDIS TANT', 'SRS_PT_BONNE', 'SRS_PT_CASSINI_SOLDNER', 'SRS_PT_CYLINDRICAL_EQUAL_AREA' , 'SRS_PT_ECKERT_I', 'SRS_PT_ECKERT_II', 'SRS_PT_ECKERT_III', 'SRS_PT_ECKERT_IV' , 'SRS_PT_ECKERT_V', 'SRS_PT_ECKERT_VI', 'SRS_PT_EQUIDISTANT_CONIC', 'SRS_PT_EQU IRECTANGULAR', 'SRS_PT_GALL_STEREOGRAPHIC', 'SRS_PT_GAUSSSCHREIBERTMERCATOR', 'S RS_PT_GEOSTATIONARY_SATELLITE', 'SRS_PT_GNOMONIC', 'SRS_PT_GOODE_HOMOLOSINE', 'S RS_PT_HOTINE_OBLIQUE_MERCATOR', 'SRS_PT_HOTINE_OBLIQUE_MERCATOR_TWO_POINT_NATURA L_ORIGIN', 'SRS_PT_IMW_POLYCONIC', 'SRS_PT_KROVAK', 'SRS_PT_LABORDE_OBLIQUE_MERC ATOR', 'SRS_PT_LAMBERT_AZIMUTHAL_EQUAL_AREA', 'SRS_PT_LAMBERT_CONFORMAL_CONIC_1S P', 'SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP', 'SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP_BE LGIUM', 'SRS_PT_MERCATOR_1SP', 'SRS_PT_MERCATOR_2SP', 'SRS_PT_MILLER_CYLINDRICAL ', 'SRS_PT_MOLLWEIDE', 'SRS_PT_NEW_ZEALAND_MAP_GRID', 'SRS_PT_OBLIQUE_STEREOGRAP HIC', 'SRS_PT_ORTHOGRAPHIC', 'SRS_PT_POLAR_STEREOGRAPHIC', 'SRS_PT_POLYCONIC', ' SRS_PT_ROBINSON', 'SRS_PT_SINUSOIDAL', 'SRS_PT_STEREOGRAPHIC', 'SRS_PT_SWISS_OBL IQUE_CYLINDRICAL', 'SRS_PT_TRANSVERSE_MERCATOR', 'SRS_PT_TRANSVERSE_MERCATOR_MI_ 21', 'SRS_PT_TRANSVERSE_MERCATOR_MI_22', 'SRS_PT_TRANSVERSE_MERCATOR_MI_23', 'SR S_PT_TRANSVERSE_MERCATOR_MI_24', 'SRS_PT_TRANSVERSE_MERCATOR_MI_25', 'SRS_PT_TRA NSVERSE_MERCATOR_SOUTH_ORIENTED', 'SRS_PT_TUNISIA_MINING_GRID', 'SRS_PT_TWO_POIN T_EQUIDISTANT', 'SRS_PT_VANDERGRINTEN', 'SRS_PT_WAGNER_I', 'SRS_PT_WAGNER_II', ' SRS_PT_WAGNER_III', 'SRS_PT_WAGNER_IV', 'SRS_PT_WAGNER_V', 'SRS_PT_WAGNER_VI', ' SRS_PT_WAGNER_VII', 'SRS_UA_DEGREE', 'SRS_UA_DEGREE_CONV', 'SRS_UA_RADIAN', 'SRS _UL_CHAIN', 'SRS_UL_CHAIN_CONV', 'SRS_UL_FOOT', 'SRS_UL_FOOT_CONV', 'SRS_UL_LINK ', 'SRS_UL_LINK_CONV', 'SRS_UL_METER', 'SRS_UL_NAUTICAL_MILE', 'SRS_UL_NAUTICAL_ MILE_CONV', 'SRS_UL_ROD', 'SRS_UL_ROD_CONV', 'SRS_UL_US_FOOT', 'SRS_UL_US_FOOT_C ONV', 'SRS_WGS84_INVFLATTENING', 'SRS_WGS84_SEMIMAJOR', 'SRS_WKT_WGS84', 'Spatia lReference', 'SpatialReference_swigregister', 'UseExceptions', '__builtins__', ' __doc__', '__file__', '__name__', '__package__', '_newclass', '_object', '_osr',

'_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setatt

r_nondynamic']