You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: module6/lab6/README.md
+74-109
Original file line number
Diff line number
Diff line change
@@ -1,59 +1,19 @@
1
1
# Module 6, Lab 6
2
2
3
-
<!-- ## Before the Lab
4
-
We need to install new packages for this lab. Install packages could take time. It is highly suggest that you do before the lab.
3
+
## Before the Lab
5
4
6
-
```bash
7
-
conda activate asm591
8
-
conda install -c conda-forge geopandas
9
-
conda install pyarrow
10
-
conda install geojson
5
+
We need to install new packages for this lab.
11
6
12
-
```
13
-
It may be possible that these packages will have difficulty being installed in the environment you have been using for previous labs. If this is the case, you will need to create a new environment. To do so, follow these steps:
14
-
1. Go to the base environment, either by deactivating your asm591 environment or by restarting your miniconda prompt.
15
-
2. Create a new environment by typing the following (replace the name with one of your choosing):
16
-
```bash
17
-
conda create --name example_name
18
-
```
19
-
3. Activate your new environment.
20
-
4. Set the channel priorities to favor Conda Forge installation:
21
-
```bash
22
-
conda config --add channels conda-forge
23
-
conda config --set channel_priority strict
24
-
```
25
-
5. Install geopandas:
26
-
```bash
27
-
conda install -c conda-forge geopandas
28
-
```
29
-
6. Then, install django:
30
-
```bash
31
-
conda install django
32
-
```
33
-
7. Then, install pyarrow:
34
7
```bash
35
-
conda install pyarrow
36
-
```
37
-
8. Then, install geojson:
38
-
```bash
39
-
conda install geojson
8
+
pip install pyarrow
9
+
pip install geojson
10
+
40
11
```
41
-
Once these are installed, you should be able to complete lab 6 with this environment. If you are still having trouble with the package installation, please let us know ASAP. -->
42
12
43
13
### Leaflet
44
14
45
15
In this lab, we will use Leaflet as a map tool. Leaflet is an open-source interactive map library. It is written in JavaScript. Therefore, it can be used in web platform including Django. More information about Leaflet and documents is here: https://leafletjs.com/index.html.
46
16
47
-
### Lab Submission Folder Structure
48
-
49
-
You will be submitting a few different things. Create a folder called **'lab6'** and copy over acrelog (including the acre folder) from lab 5.
50
-
51
-
```
52
-
lab6/
53
-
acre/ # Copy over your previous project, you'll be building on it.
54
-
README.md
55
-
```
56
-
57
17
## Embed Leaflet Map in Django
58
18
59
19
First, we will import Leaflet script to our html page by adding the script below into the head section of your template file (.html).
@@ -62,112 +22,97 @@ First, we will import Leaflet script to our html page by adding the script below
There are 4 scripts that we import here. The fisrt two are Leaflet's stylesheet and JavaScript modules. There two keep all functionalities for runing Leaflet.
There are 3 scripts that we import here. The first two are Leaflet's stylesheet and JavaScript modules. The last file is the JavaScript that we will create and define our map's behaviors.
109
53
110
-
Note that we import with `static` in front of the custom script. In Django, Javascript and stylesheets files will be kept in `static` folder in your application. We also need to tell Django that we will use `static` folder by adding one line at the top of html file as below.
54
+
Note that we import with `static` in front of the custom script. In Django, Javascript (and stylesheet) files will be kept in `static` folder in your application. We also need to tell Django that you will use `static` folder by adding one line at the top of html file as below.
111
55
112
56
```html
113
-
{% load static %} <--addthisline
57
+
{% load static %}
58
+
<!-- add this line -->
114
59
<!DOCTYPE html>
115
60
<htmllang="en">
116
61
...
117
62
</html>
118
63
```
119
64
120
-
Let's create a static folder in your application folder. Then create `leaflet-map.js` and `leaflet-map.css`. Copy the following code in `leaflet-map.js`
65
+
Let's create a static folder (if you have not done already) in your application folder. Then create `leaflet-map.js`. Copy the following code in `leaflet-map.js`
// This map will be placed on a HTML tag that has id "map"
127
79
constmap=L.map("map", { layers: [layer] });
128
80
81
+
// We will tell the map to show the following location as a default.
82
+
// Otherwise, the starting view will be somewhere in Atlantic ocean
129
83
map.fitBounds([
130
84
[40.470060621973026, -86.99269856365936], // Somewhere in ACRE
131
85
// You can add more coordinates
132
86
]);
133
87
```
134
88
135
-
We will be using OpenStreetMap layer. So, ther first line is to declear the source of our data. The second line is the URL of the default OpenStreetMap. There are many more options that you can use from [here](https://leaflet-extras.github.io/leaflet-providers/preview/). The third line will load the layer from providing URL. Then the forth line will put that layer into our map.
89
+
We will be using OpenStreetMap layer. So, the first line is to declare the source of our data. The second line is the URL of the default OpenStreetMap. There are many more options that you can use from [here](https://leaflet-extras.github.io/leaflet-providers/preview/). The third line will load the layer from providing URL. Then the forth line will put that layer into our map.
136
90
137
91
One last thing for the JavaScript file is to choose the default view. That is when `map.fitBounds()` comes to play. You can pass coordinate or a list of coordinates that the map will show when you reload it.
138
92
139
-
Next is the stylesheet. Open `leaflet-map.css` and copy the following code. For now, we will display `body` tag with 100% screen height. Then the tag with id=`map` will also be 100% of height and width. You can change this configuration later.
140
-
141
-
```css
142
-
html,
143
-
body {
144
-
height: 100%;
145
-
margin: 0;
146
-
}
147
-
#map {
148
-
height: 100%;
149
-
width: 80vw;
150
-
}
151
-
```
152
-
153
93
Now, we are ready to show the map. Choose one of your existing html files (besides the template) or create a new one to place the map in. Add the following line into the body part.
154
94
155
95
```html
156
96
<body>
157
-
...
158
-
<divid="map"></div>
159
-
<--addthisline...
97
+
<!-- Other lines in your body -->
98
+
99
+
<!-- Add this line in to the body part of your html -->
Lastly, you will need to config `url.py` and `views.py` to point to the template file we just created. The following snippet assumes you have placed it in the index.html file, so swap out the references to the index with your page if you chose elsewhere.
107
+
Lastly, you will need to config `url.py` and `views.py` to point to the template file we just created. The following snippet assumes you have placed it in the new page called `map.html`.
164
108
165
109
```python
166
110
# views.py
167
111
from django.shortcuts import render
168
112
113
+
# Add this function
169
114
defrender_map(request):
170
-
return render(request, "index.html")
115
+
return render(request, "map.html")
171
116
```
172
117
173
118
```python
@@ -176,13 +121,16 @@ from django.urls import path
176
121
from . import views
177
122
178
123
urlpatterns = [
179
-
path("", views.render_map, name="index"),
124
+
# Your other patterns
125
+
path("map", views.render_map, name="map"), # add new one
180
126
]
181
127
```
182
128
129
+
Now, when you go to `127.0.0.1:8000/acrelog/map`, you should see the map.
130
+
183
131
## Putting Features on the Map
184
132
185
-
Leaflet allows us to modify the map freely. One of the common things to put into the map is a marker. Techically speaking, a marker is a point (geometric object) on the map. To add a marker, first add the following line into `leaflet-map.js`.
133
+
Leaflet allows us to modify the map freely. One of the common things to put into the map is a marker. Technically speaking, a marker is a point (geometric object) on the map. To add a marker, first add the following line into `leaflet-map.js`.
186
134
187
135
```javascript
188
136
let marker =L.marker([40.470060621973026, -86.99269856365936]).addTo(map);
@@ -208,23 +156,30 @@ def render_map(request):
208
156
point = geo.Point(([-86.99269856365936, 40.470060621973026]))
First, we import `geojson` and `shapely.geometry`. We will use `shapely.geometry` to create geometry objects. In this case, we want to create a marker that is a point. Then we create a geoJSON feature object by passing the geometry and properties. The properties parameter accepts a dictionary. Therefore, you can have multiple key-value properties. Next, we wrap the marker into a geoJSON feature collection. A GeoJSON feature collection is a list of geoJSON feature. You can pass a list of geoJSON features. However, in this example, we only pass one marker. Lastly, we return the render page with additional information going back to the HTML side.
162
+
First, we import `geojson` and `shapely.geometry`. We will use `shapely.geometry` to create geometry objects. In this case, we want to create a marker that is a point. Then we create a geoJSON feature object by passing the geometry and properties. The `properties` parameter accepts a dictionary. Therefore, you can have multiple key-value properties. Next, we wrap the marker into a geoJSON feature collection. A geoJSON feature collection is a list of geoJSON feature. You can pass a list of geoJSON features. However, in this example, we only pass one feature (a marker). Lastly, we return the render page with additional information going back to the HTML side.
215
163
216
-
Open `index.html` file and modify as below. We interpolate date from Django by wrapping it with double curly brackets. Then we give HTML a clue that this is a JSON data. Lastly, we name this data "data_geojson" for HTML and JavaScript side.
164
+
Open `map.html` file and modify as below. We interpolate date from Django by wrapping it with double curly brackets. Then we give HTML a clue that this is a JSON data. Lastly, we name this data `data_geojson` for HTML and JavaScript side.
Next we need to process the geoJSON data in JavaScript file. Open `leaflet-map.js` then add the code below. First, we parse the data from HTML. As the data is in geoJSON format, we can use Leaflet built-in function to display all features in the geoJSON feature collection. We can also bind a popup box with message that we want to show. In this case, it is a `message` property that we embed from Python side (`view.py`). Finally, we add the feature to the map.
174
+
Next, we need to process the geoJSON data in JavaScript file. Open `leaflet-map.js` then add the code below. First, we parse the data from HTML. As the data is in geoJSON format, we can use Leaflet built-in function to display all features in the geoJSON feature collection. We can also bind a popup box with message that we want to show. In this case, it is a `message` property that we embed from Django side (`view.py`). Finally, we add the feature to the map.
226
175
227
176
```javascript
177
+
// You can delete this part. We move this marker creation to Django's side.
178
+
let marker =L.marker([40.470060621973026, -86.99269856365936])
@@ -236,30 +191,40 @@ let feature = L.geoJSON(data.features)
236
191
237
192
You can add multiple geometry objects into one geoJSON feature collection. The objects could be points, lines, or polygons. The most of the data processing will be done in `view.py`. Whatever data you embed into rendering process will be passed to HTML and JavaScript side for display into the map.
238
193
239
-
## Display ACRE map
194
+
## Your turn: display ACRE map
240
195
241
196
You will see `acre_geometry.parquet` file in the data folder. Parquet file (.parquet) is a compressed file (like a zip file). Inside, you will see the geometry of fields in ACRE. You can use `geopandas` to read this file by `read_parquet("filename.parquet")`.
242
197
243
-
Your task is:
198
+
**Your tasks are**:
244
199
245
200
- Create a map of ACRE with field's boundaries.
246
201
- When you click on the field, the map will show a popup with a brief summary of the latest operation on that field and a link to view the full detail of that field.
247
202
203
+
### Lab Submission Folder Structure
204
+
205
+
You will be submitting a few different things. Create a folder called **'lab6'** and copy over acrelog (including the acre folder) from lab 5.
206
+
207
+
```
208
+
lab6/
209
+
acre/ # Copy over your previous project, you'll be building on it.
210
+
README.md
211
+
```
212
+
248
213
## Submitting your work
249
214
250
-
Make sure to save your notebook code after completing all the steps. Remember to use the git commands "add", "commit", and finally "push" to add your files, commit the changes with a comment, and push the changes to the Github website. Also remember, you should have a commit history with at least 5 commits to demostrate ongoing effort (don't just commit it all 5 mins before it's due!).
215
+
Make sure to save your notebook code after completing all the steps. Remember to use the git commands "add", "commit", and finally "push" to add your files, commit the changes with a comment, and push the changes to the Github website. Also remember, you should have a commit history with at least 5 commits to demonstrate ongoing effort (don't just commit it all 5 mins before it's due!).
251
216
252
217
GO TO BRIGHTSPACE, submit the link to your repository. You are done!
253
218
254
219
## Future Learning Pathways
255
220
256
221
Check the lecture from previous iterations of this course
You can work on `exercise.ipynp`. This is optional but highly recommended if you want to learn how to work with geospatial data.
227
+
You can work on `exercise.ipynb`. This is optional but highly recommended if you want to learn how to work with geospatial data.
263
228
264
229
The [Shapely documentation](https://shapely.readthedocs.io/en/stable/manual.html) is actually an excellent place to explore geometric operations. You can learn more than you ever thought possible about boundaries, geometric objects and how to work with them. **Really!** It has been well written! It is less code documentation and more of an introduction to the basics of geometry. If you are in Precision Agriculture, or another field in which geometry is important, you really should understand the issues it discusses. For example, a line may "contain" a point, but it doesn't "cross" the point. Also, lines that "touch" do not "overlap" and lines that "overlap" do not "touch." We are not very precise when we discuss these ideas, but programming requires a degree of precision that can be very useful to understand. The documentation makes sure to mention these gotchas between how we think and talk about geometry.
0 commit comments