### Site Tools

wiki:python:basicpython2

# Differences

This shows you the differences between two versions of the page.

 — wiki:python:basicpython2 [2018/06/02 09:54] (current) Line 1: Line 1: + ====== Geospatial Python crash course, part 2 ====== + + ===== Some examples of code ===== + + Some example of reasonable complicated programs: + + ​ + + import turtle as t + + # DATA MODEL + # All layers will have a name, 1+ points, and population count + NAME = 0 + POINTS = 1 + POP = 2 + + # Create the state layer + state = ["​COLORADO",​ [[-109, 37], [-109, 41], [-102, 41], [-102, 37]], 5187582] + + # Cities layer list + # city = [name, [point], population] + cities = [] + + # Add Denver + cities.append(["​DENVER",​[-104.98,​ 39.74], 634265]) + # Add Boulder + cities.append(["​BOULDER",​[-105.27,​ 40.02], 98889]) + # Add Durango + cities.append(["​DURANGO",​[-107.88,​37.28],​ 17069]) + + # MAP GRAPHICS RENDERING + map_width = 800 + map_height = 500 + + # State Bounding Box + # Use Python min/max function to get bounding box + minx = 180 + maxx = -180 + miny = 90 + maxy = -90 + for x,y in state[POINTS]:​ + if x < minx: minx = x + elif x > maxx: maxx = x + if y < miny: miny = y + elif y > maxy: maxy = y + # Get earth distance on each axis + dist_x = maxx - minx + dist_y = maxy - miny + + # Scaling ratio each axis + # to map points from world to screen + x_ratio = map_width / dist_x + y_ratio = map_height / dist_y + + # Function to convert lat/lon to screen coordinates + def convert(point):​ + lon = point[0] + lat = point[1] + x = map_width - ((maxx - lon) * x_ratio) + y = map_height - ((maxy - lat) * y_ratio) + # Python turtle graphics start in the middle of the screen + # so we must offset the points so they are centered + x = x - (map_width/​2) + y = y - (map_height/​2) + return [x,y] + + # Draw the state + t.up() + first_pixel = None + for point in state[POINTS]:​ + pixel = convert(point) + if not first_pixel:​ + first_pixel = pixel + t.goto(pixel) + t.down() + # Go back to the first point + t.goto(first_pixel) + # Label the state + t.up() + t.goto([0,​0]) + t.write(state[NAME],​ align="​center",​ font=("​Arial",​16,"​bold"​)) + + # Draw the cities + for city in cities: + pixel = convert(city[POINTS]) + t.up() + t.goto(pixel) + # Place a point for the city + t.dot(10) + # Label the city + t.write(city[NAME] + ", Pop.: " + str(city[POP]),​ align="​left"​) + t.up() + + # Perform an attribute query + # Question: Which city has the largest population? + # Write the result but make sure it's under the map + biggest_city = max(cities, key=lambda city:​city[POP]) + t.goto(0, -1*((map_height/​2)+20)) + t.write("​The biggest city is: " +  biggest_city[NAME]) + + # Perform a spatial query + # Question: Which is the western most city? + # Write the result but make sure it's under the other question + western_city = min(cities, key=lambda city:​city[POINTS]) + t.goto(0, -1*((map_height/​2)+40)) + t.write("​The western-most city is: " + western_city[NAME]) + + # Hide our map pen + t.pen(shown=False) + t.done() + + ​ + + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​02/​fmtDecode.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​02/​GeoJSON.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​02/​structDemo.py }} + + ===== Playing with network ===== + + Python is a general purpose language, so it can be used for network applications. + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​01_urlretrieve.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​02_earthquake-data.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​03_ftp.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​04_ftp-url.py }} + + ===== Zip files and others ===== + + It can be used to manipulate archive files: + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​05_zipfile.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​06_zipfile-loop.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​07_tarfile-create.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​08_tarfile-extract.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​09_cloud-zipfile.py }} + + + ===== Playing with XML ===== + + + Many useful file formats are of XML type (e.g. KML) and Python has a good deals of modules to manage them: + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​10_kml-minidom.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​01_kml-etree.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​12_make-kml-strings.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​13_make-kml-etree.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​14_gpx-soup.py }} + + ===== Playing with some geospatial modules ===== + + Some very basic geospatial snippets of code; + + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​15_shapely-wkt.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​16_ogr-wkt.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​17_json.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​18_geojson.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​19_ogr.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​20_pyshp.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​21_dbfpy.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​22_shapely.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​23_gdal.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​24_numpy.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​25_PIL.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​26_PNGC.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​04/​27_PDF.py }} + + ===== Using OGR ===== + + Let us see some very basic useful snippets of OGR code which could be combined together to build more complicated things: + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​03/​01_ogr.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​03/​02_ogr.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​03/​03_ogr.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​03/​04_ogr.py }} + + Playing with projection in OGR and UTM modules: + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​06-utm2latlon.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​07-latlon2utm.py }} + + //Hands on work: write a couple of programs to convert coordinates taken from input (hint: sys.stdin), command line, etc. + // + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​08-reprojection.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​09-read-shp.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​10-change-shp.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​11-add-field.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​12-merge.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​13-split.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​14-pip.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​15-attr-sel.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​16-dot-density.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​17-choropleth.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​18-excel.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​05/​19-nmea.py }} + + + ===== Playing with rasters ===== + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​01-swap-bands.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​02-histogram.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​03-stretch.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​04-clip.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​05-classify.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​06-threshold.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​07-extract.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​08-draw.py }} + + {{gh> https://​github.com/​fpl/​geotutorial_basic/​blob/​master/​06/​09-change.py }} + + + + + + ---- + + ===== Some introductory readings ===== + + [[http://​en.wikipedia.org/​wiki/​Flood_fill|Flood fill algotithms]] + + [[http://​en.wikipedia.org/​wiki/​Dijkstra'​s_algorithm|Dijkstra algorithm]] + + [[http://​en.wikipedia.org/​wiki/​A-star_algorithm|A* simple algorithm]] + + [[https://​www.wunderground.com/​weather/​api/​|Weather underground API]] +
wiki/python/basicpython2.txt ยท Last modified: 2018/06/02 09:54 (external edit)