Search code examples
djangopython-3.xgeodjangopostgis-installation

Error was: cannot import name 'GDALRaster' on Windows 10 for GeoDjango


I am trying to follow GeoDjango Tutorial . I am using a windows-10 64bit computer. and I am using Python 3.6 and Django 1.11 I followed the GeoDjango Windows Documentation to install. I have successfully completed all the steps it told me to I also downloaded and ran OSGeo4W and successfully ran the paths in my cmd.exe as administrator

enter image description here

Now getting back to the Django Tutorial

Create a Spatial Database: I made a Database in my Postgres Sql

enter image description here

I started a Django project called geodjango, Started a app called world

I changed the database in the setting to

DATABASES = {
    'default': {
         'ENGINE': 'django.contrib.gis.db.backends.postgis',
         'NAME': 'geodjango',
         'USER': 'geo',
    },
}

I added the below in my settings.py installed apps apps

'django.contrib.gis',
'world',

I made a directory called data in the world application. I downloaded the zip file and unzipped it in my data folder. I have the below files in my data folder enter image description here

Use ogrinfo to examine spatial data(I dint know what to do here, So I ignored this part)

I successfully made the models.py like it says in the tutorial

from django.contrib.gis.db import models

class WorldBorder(models.Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.IntegerField('Population 2005')
    fips = models.CharField('FIPS Code', max_length=2)
    iso2 = models.CharField('2 Digit ISO', max_length=2)
    iso3 = models.CharField('3 Digit ISO', max_length=3)
    un = models.IntegerField('United Nations Code')
    region = models.IntegerField('Region Code')
    subregion = models.IntegerField('Sub-Region Code')
    lon = models.FloatField()
    lat = models.FloatField()

    # GeoDjango-specific: a geometry field (MultiPolygonField)
    mpoly = models.MultiPolygonField()

    # Returns the string representation of the model.
    def __str__(self):              # __unicode__ on Python 2
        return self.name

Then the tutorial asks me to run python manage.py makemigrations when I do that I get the below error

Error was: cannot import name 'GDALRaster'

What can I do to fix this error

I tried

pip install gdal 

that gave me a error

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools 

so I downloaded Microsoft Visual C++ Build Tools. and tried to run

 pip install gdal 

again Now I get the error

`error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x64\\cl.exe' failed with exit statu
s 2`

Solution

  • After trying almost everything. I moved my os from windows to Linux with Ubuntu 18 (Udemy course to learn Ubuntu) through virtual box then simply did

    sudo apt install geos sudo apt install prog4 sudo apt install gdal

    installed postgres sql and pgadmin3 (pg admin 4 is not available easily for install as of today on ubuntu ) made the database on called 'geodjango' using pgadmin3 then sudo apt install postgis

    then pip installed psycopg2 and dj-database-url

    I would recommend to stay away from the make & make install methods they take too long and always throws errors. After this the GeoDjango tutorial was a breeze