Lecture 5 Files, modules Programming in geoinformatics Autumn 2017 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files Homework Exercise 1 Exercise 1 Load the file lines.json into Python and calculate their length using lineTools.py module we created in the previous seminar. Your new script.py, module lineTools.py and lines.json file all have to be saved in the same directory! lines.json: [[[199.47, 474.5], [381.91, 529.65], [480.38, 211.76], [460.02, 717.44], [372.44, 573.33], [823.79, 450.65], [883.74, 816.62], [339.27, 247.52]], [[67.59, 652.63], [404.5, 435.38], [666.35, 6.46], [898.26, 238.47], [298.32, 433.35], [526.2, 358.93], [769.27, 493.64], [921.44, 506.83], [210.55, 556.11], [934.96, 329.52], [778.76, 690.23], [711.17, 471.51]], [[283.6, 360.42], [727.24, 96.59]]] Hint: use the function loads(data) from json module to get the lines from a string. Autumn 2017 Lecture 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files Homework Homework 1 Homework 2 Bonus Homework Homework 1 Expanding on the exercise 1, create a new file statistics.txt with statistics for the lines in lines.json. Use a format like this: Line 1 – length: 6.67834; center: 6.7, 9.3; number of nodes: 8 Line 2 – length: 5.37627; center: 0.3, 6.4; number of nodes: 4 Line 3 – length: 2.82738; center: 7.1, -2.8; number of nodes: 2 Note: you should also add another function pointCenter(points) to the lineTools.py module. You can expand on the Homework 1 from Seminar 2, but you need to use loops. Note I have also put an example script to study materials, which you can use to open files using relative paths – it should work straight away! Autumn 2017 Lecture 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files Homework Homework 1 Homework 2 Bonus Homework Homework 2 Again, using the lineTools.py module, generate 10 random lines and save them as a new file (e.g. lines_hw2.json). The structure will be the same as in lines.json in exercise 1 and 2. Note: try adjusting the randomLine() function to also round point coordinates to 2 decimal places. Autumn 2017 Lecture 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Files Homework Homework 1 Homework 2 Bonus Homework Bonus Homework Following up on the previous exercise, format the 10 random lines as GeoJSON (see theory_05.pdf for specification and examples). Example: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [199.47, 474.5], [381.91, 529.65], [480.38, 211.76], [460.02, 717.44], [372.44, 573.33], [823.79, 450.65], [883.74, 816.62], [339.27, 247.52]→ ] } }, { … line 2 … }, … { … line10 … } ] } Autumn 2017 Lecture 5