/
leerme.txt
241 lines (220 loc) · 8.5 KB
/
leerme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Tutorial de desarrollo de aplicaciones web geoespaciales con Python y Django
Este es el procedimiento completo en Windows con Anaconda:
1 - Abra su indicador de Anaconda e instale Django y folium.
pip install Django
pip install folium
2 - Vaya a la ubicación donde desea que se cree su proyecto
3 - Crea el proyecto “geo”
4 - Mover al directorio creado
cd geo
5 - Crea la aplicación Django “geoApp”
python administrar.py iniciar aplicación geoApp
6 - Crea el archivo “urls.py” dentro de geoApp
7- Dentro de “geoApp/urls.py”, pegue las siguientes líneas
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('', views.home,name='home'),
]
8 - Ahora tenemos que registrar este “geoApp/urls.py” dentro de “geo/urls.py”, agregar “incluir” a “Django.urls”
from django.urls import path, include
agregue la siguiente línea a "urlspatterns"
path('',include('geoApp.urls')),
Ahora tienes algo como esto:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('geoApp.urls')),
]
9 - Dentro de “geoApp”:
Crea una carpeta llamada “templates”
Dentro de “templates” crea una carpeta llamada “geoApp”
10 - Antes de configurar los archivos “HTML”, definamos los archivos “static” y la “path”. En “/geo/settings.py”
Primero, en la parte superior del archivo agregue
import os
busque "STATIC_URL", se encuentra al final del archivo y reemplácelo con los siguientes comandos
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
11 - Agrega las carpetas “static” y “media” en la carpeta raíz:
12 - Dentro de la carpeta “static”, pegue la carpeta “dist” que está dentro del Framework “bootstrap” (https://github.com/twbs/bootstrap/dist/), renombrándola y en “jquery”, el archivo “jquery-3.7.1.slim.min.js” de (https://jquery.com/download/).
13 - Regrese a “settings.py”, y en “INSTALLED_APPS” agregue:
'geoApp',
14 - Continuando en el archivo “settings.py”, en “DIRS” dentro de “TEMPLATES” agregue
os.path.join(BASE_DIR, 'geoApp','templates'),
Tendrías algo como lo siguiente:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'geoApp','templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
15 - Editar “/geoApp/views.py”, aquí creamos una función de inicio que regresa a nuestra plantilla “home.html”, esta plantilla se crea en el siguiente paso:
from django.shortcuts import render, redirect
# Crea tus vistas aquí.
# Create your views here.
def home(request):
context={}
return render(request,'geoApp/home.html',context)
16 - Crea un nuevo archivo dentro de la nueva carpeta llamada “geoApp/templates/geoApp /home.html”, pega este código HTML dentro:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>GeoGida</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="{% static '/bootstrap/css/bootstrap.min.css' %}">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
<div class="container">
<a class="navbar-brand" href="#">GeoGida</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="">Welcome</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 class="mt-5">GeoApp developed by Hatarilabs</h1>
<p class="lead">You´ll be seeing shapefiles storaged in Database</p>
<ul class="list-unstyled">
<li>Developed by Hatarilabs</li>
<li>2020</li>
</ul>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="{% static '/jquery/jquery.slim.min.js' %}"></script>
<script src="{% static '/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
</body>
</html>
17 - Hasta este punto, ve al kernel y ejecuta tu aplicación.
python manage.py runserver
Vaya a localhost y debería poder ver la siguiente interfaz:
http://127.0.0.1:8000/
18 - Ahora, agregaremos la vista geoespacial, para hacer esto copie la carpeta "shp" de los datos proporcionados a "media", esto contiene dos archivos ".geojson".
19 - Modifique el archivo “geoApp/views.py” de la siguiente manera:
from django.shortcuts import render, redirect
import os
import folium
# Create your views here.
def home(request):
shp_dir = os.path.join(os.getcwd(),'media','shp')
# folium
m = folium.Map(location=[-16.22,-71.59],zoom_start=10#)
## style
style_basin = {'fillColor': '#228B22', 'color': '#228B22'}
style_rivers = { 'color': 'blue'}
## adding to view
folium.GeoJson(os.path.join(shp_dir,'basin.geojson'),name='basin',style_function=lambda x:style_basin).add_to(m)
folium.GeoJson(os.path.join(shp_dir,'rivers.geojson'),name='rivers',style_function=lambda x:style_rivers).add_to(m)
folium.LayerControl().add_to(m)
## exporting
m=m._repr_html_()
context = {'my_map': m}
## rendering
return render(request,'geoApp/home.html',context)
20 - Ahora, agregue el mapa al código HTML, abra “home.html” y agregue después de:
“<p class="lead">Verá archivos de forma almacenados en la base de datos</p>”, el siguiente comando:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta
name="description"
content="Tutorial de desarrollo de aplicaciones web geoespaciales con Python y Django"
/>
<meta name="author" content="Guillermo Toranzo Perez" />
<title>GeoApp</title>
<!-- Bootstrap core CSS -->
<link
rel="stylesheet"
href="{% static '/bootstrap/css/bootstrap.min.css' %}"
/>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
<div class="container">
<a class="navbar-brand" href="#">GeoApp</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarResponsive"
aria-controls="navbarResponsive"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="">Bienvenido</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 class="mt-5">GeoApp desarrollada con Python</h1>
<p class="lead">
Verás archivos de forma almacenados en la Base de Datos sqlite3.
</p>
{{ my_map|safe }}
<ul class="list-unstyled">
<li>
Desarrollada por
<a href="https://github.com/gtoranzo" target="_blank">gtoranzo</a>
</li>
<li>2023</li>
</ul>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="{% static '/jquery/jquery-3.7.1.slim.min.js' %}"></script>
<script src="{% static '/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
</body>
</html>