Codes pour le cours sur les données ouvertes et conception d'API
#webservice/views.py
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from geowebapp.models import City
from .serializers import CitiesSerializers
@api_view(['GET'])
def cities(request):
villes = City.objects.all()
serializer = CitiesSerializers(villes, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
@api_view(['GET'])
def city(request, city_id):
print(city_id, request)
ville = City.objects.filter(city_id=city_id).first()
serializer = CitiesSerializers(ville)
return Response(serializer.data, status=status.HTTP_200_OK)
Run to view results
#webservice/urls.py
from django.contrib import admin
from django.urls import path, re_path
from .views import cities, city
urlpatterns = [
path("", cities, name="all_cities"),
path("cities/", cities, name="cities"),
path("cities/<int:city_id>/", city, name="city"),
#re_path(r'^cities/(?P<city_id>\d+)/', city, name="city"),
]
Run to view results
#webservice/serializers.py
from rest_framework import serializers
from rest_framework_gis.serializers import GeoFeatureModelSerializer
from geowebapp.models import City
class CitiesSerializers(GeoFeatureModelSerializer):
""" A class to serialize locations as GeoJSON compatible data """
class Meta:
app_label = 'geowebapp'
model = City
geo_field = "location"
fields = ('city_id', 'city_name', 'country_id_id')
Run to view results
#geodjango_demo/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include('geowebapp.urls')),
path("admin/", admin.site.urls),
path("geowebapp/", include('geowebapp.urls')),
path("webservice/", include('webservice.urls')),
]
Run to view results
#geowebapp/views.py
class GEEView(TemplateView):
credentials = ee.ServiceAccountCredentials(
'@ee-....iam.gserviceaccount.com',
'./ee-....json'
)
ee.Initialize(credentials)
template_name = 'map.html'
# Define a method for displaying Earth Engine image tiles on a folium map.
def get_context_data(self, **kwargs):
#figure = folium.Figure()
# create Folium Object
m = folium.Map(
location=[53.0372, -103.8881],
zoom_start=8
)
# add map to figure
#m.add_to(figure)
# select the Dataset Here's used the MODIS data
dataset = (ee.ImageCollection('MODIS/006/MOD13Q1')
.filter(ee.Filter.date('2019-07-01', '2019-11-30'))
.first())
modisndvi = dataset.select('NDVI')
# Styling
vis_paramsNDVI = {
'min': 0,
'max': 9000,
'palette': ['FE8374', 'C0E5DE', '3A837C', '034B48', ]}
# add the map to the the folium map
map_id_dict = ee.Image(modisndvi).getMapId(vis_paramsNDVI)
# GEE raster data to TileLayer
folium.raster_layers.TileLayer(
tiles=map_id_dict['tile_fetcher'].url_format,
attr='Google Earth Engine',
name='NDVI',
overlay=True,
control=True
).add_to(m)
dataset = (ee.ImageCollection('NRCan/CDEM').first())
#.filter(ee.Filter.date('2016-01-01', '2016-12-31'))
#
crop = dataset.select('elevation')
elevationVis = {
'min': -50.0,
'max': 1500.0,
'palette': ['0905ff', 'ffefc4', 'ffffff'],
}
map_id_dict = ee.Image(crop).getMapId(elevationVis)
# GEE raster data to TileLayer
folium.raster_layers.TileLayer(
tiles=map_id_dict['tile_fetcher'].url_format,
attr='Google Earth Engine',
name='Elevation',
overlay=True,
control=True
).add_to(m)
# add Layer control
m.add_child(folium.LayerControl())
# figure
#figure.render()
# return map
return {"map": m._repr_html_()}
Run to view results
#geowebapp/urls.py
from django.contrib import admin
from django.urls import path
from .views import MainView, CitiesView, CountriesView, GEEView
urlpatterns = [
path("", MainView.as_view(), name="home"),
path("cities/", CitiesView.as_view(), name="cities"),
path("countries/", CountriesView.as_view(), name="countries"),
path("gee/", GEEView.as_view(), name="gee"),
]
Run to view results