CAVaccineInventory / vial

The Django application powering calltheshots.us
https://vial.calltheshots.us
MIT License
13 stars 1 forks source link

Research project: GeoDjango + PostGIS for county shapes #79

Closed simonw closed 3 years ago

simonw commented 3 years ago

It may well be useful for us to start using GeoDjango and PostGIS in the future. This issue is mainly to record research along those lines.

simonw commented 3 years ago

To enable PostGIS we can run this migration:

from django.contrib.postgres.operations import CreateExtension
from django.db import migrations

class Migration(migrations.Migration):
    dependencies = [
        ("core", "0034_location_import_json_import_ref"),
    ]
    operations = [
        CreateExtension("postgis"),
    ]

Then apply this change to our core/models.py:

diff --git a/vaccinate/core/models.py b/vaccinate/core/models.py
index 0a8b900..7abfed2 100644
--- a/vaccinate/core/models.py
+++ b/vaccinate/core/models.py
@@ -3,6 +3,7 @@ import uuid

 import pytz
 from django.db import models
+from django.contrib.gis.db import models as gis_models
 from django.utils import dateformat, timezone

 from .baseconverter import pid
@@ -81,7 +82,7 @@ class State(models.Model):
         db_table = "state"

-class County(models.Model):
+class County(gis_models.Model):
     """
     Every part of California is in one of the state's 58 counties, which are also the primary unit that coordinates
     vaccinations and sets vaccination policies. A county's policies may not apply to every location in the county if the
@@ -110,6 +111,8 @@ class County(models.Model):
         help_text="Airtable record ID, if this has one",
     )

+    geometry = gis_models.GeometryField(null=True, blank=True)
+
     def __str__(self):
         return self.name

And run this migration:

import django.contrib.gis.db.models.fields
from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
        ("core", "0035_postgis"),
    ]

    operations = [
        migrations.AddField(
            model_name="county",
            name="geometry",
            field=django.contrib.gis.db.models.fields.GeometryField(
                blank=True, null=True, srid=4326
            ),
        ),
    ]

Add "django.contrib.gis" to INSTALLED_APPS to get a map editing interface in the Django admin for counties.

simonw commented 3 years ago

I'm going to avoid far-future research issues like this for the moment.