1

I want to do the functionality of finding the places near me. It's about restaurants, so I have my location (latitude, longitude), and I need to get the closest restaurants within a given radius.

I'm using django 2.1.7 and postgres 11.2. For the geolocation and geospacial queries postgis extension.

On my computer it's working properly.

This is the snippet of my code:

from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D

point = Point(float(latitude), float(longitude))
query = Restaurant.objects.filter(location__dwithin=(point, D(km=km))).order_by('location')

The configuration for DB is:

# [START db_setup]
if os.getenv('GAE_APPLICATION', None):
    # Running on production App Engine, so connect to Google Cloud SQL using
    # the unix socket at /cloudsql/<your-cloudsql-connection string>
    DATABASES = {
        'default': {
            'ENGINE': 'django.contrib.gis.db.backends.postgis',
            'HOST': '/cloudsql/blog:us-central1:mb',
            'USER': 'user',
            'PASSWORD': 'password',
            'NAME': 'blog',
            'PORT': '5432'
        }
    }
else:
    # Running locally so connect to either a local MySQL instance or connect to
    # Cloud SQL via the proxy. To start the proxy via command line:
    #
    #     $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
    #
    # See https://cloud.google.com/sql/docs/mysql-connect-proxy
    DATABASES = {
        'default': {
            'ENGINE': 'django.contrib.gis.db.backends.postgis',
            'HOST': '127.0.0.1',
            'USER': 'user',
            'NAME': 'db_name',
            'PORT': '5432',
        }
    }
# [END db_setup]

The deployment is running on a GAE instance, and the postgres is on a Cloud SQL instance.

When I do the deploy the page throws a 502 Bad Gateway and the command gcloud app logs tail -s default says:

2019-03-29 00:52:41 default[20190328t173044] Traceback (most recent call last): File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker worker.init_process() File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 104, in init_process super(ThreadWorker, self).init_process() File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process self.load_wsgi() File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi self.wsgi = self.app.wsgi() File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load return self.load_wsgiapp() File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp return util.import_app(self.app_uri) File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app __import__(module) File "/srv/main.py", line 1, in <module> from marcobetetaapi.wsgi import application File "/srv/marcobetetaapi/wsgi.py", line 16, in <module> application = get_wsgi_application() File "/env/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application django.setup(set_prefix=False) File "/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/env/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate app_config.import_models() File "/env/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/srv/recommendations/models.py", line 1, in <module> from django.contrib.gis.db import models File "/env/lib/python3.7/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module> import django.contrib.gis.db.models.functions # NOQA File "/env/lib/python3.7/site-packages/django/contrib/gis/db/models/functions.py", line 4, in <module> from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField File "/env/lib/python3.7/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module> from django.contrib.gis import forms, gdal File "/env/lib/python3.7/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module> from .fields import ( # NOQA File "/env/lib/python3.7/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module> from django.contrib.gis.geos import GEOSException, GEOSGeometry File "/env/lib/python3.7/site-packages/django/contrib/gis/geos/__init__.py", line 5, in <module> from .collections import ( # NOQA File "/env/lib/python3.7/site-packages/django/contrib/gis/geos/collections.py", line 9, in <module> from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin File "/env/lib/python3.7/site-packages/django/contrib/gis/geos/geometry.py", line 8, in <module> from django.contrib.gis import gdal File "/env/lib/python3.7/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module> from django.contrib.gis.gdal.datasource import DataSource File "/env/lib/python3.7/site-packages/django/contrib/gis/gdal/datasource.py", line 39, in <module> from django.contrib.gis.gdal.driver import Driver File "/env/lib/python3.7/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module> from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi File "/env/lib/python3.7/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module> from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal File "/env/lib/python3.7/site-packages/django/contrib/gis/gdal/libgdal.py", line 43, in <module> % '", "'.join(lib_names) django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal", "GDAL", "gdal2.2.0", "gdal2.1.0", "gdal2.0.0", "gdal1.11.0", "gdal1.10.0", "gdal1.9.0"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

I've reading previews questions about the same problem, and they say that GDAL library is not supported by GAE, but those questions are from years ago. I'd like to know if know it's supported or what could I change to make it works again using django and postgres.

Lety Cruz
  • 133
  • 1
  • 16

0 Answers0