GenericCsvToPolygon.py

From Open Foris Wiki

Jump to: navigation, search

Program for creating polygons from text files.

Usage: genericCsvToPolygon.py <input.csv> <output.shp>

  • The program is modified form the one by Chris Garrard
  • The input file is a text file of the following format: Polygon_id:corner coordinates in WGS84 system
  • Coordinate pairs are separated from others with a space and x,y with a comma. An example is shown below:

bushland1:38.99408253760913,-11.04146530113384,0 38.99380823486723,-11.04205402821617,0 38.99380826389991,-11.04206992654894,0 38.99382544867113,-11.04261044223288,0 38.9938254776416,-11.04262634062336,0 38.99415014990515,-11.04300732377466,0 38.9941664064954,-11.04303909164155,0 38.99466885692982,-11.04319717791531,0 38.99473365203311,-11.04319706202726,0 38.99479844656671,-11.0431969461398,0 38.99515464117336,-11.04310091874687,0 38.99518697983437,-11.04306906417552,0 38.99546174305614,-11.04273470777748,0 38.99551022187544,-11.04267102800237,0 38.9955263329932,-11.04262330437018,0 38.99563902187934,-11.0422415447644,0 38.99563896353241,-11.04220974840945,0 38.99573545258073,-11.04182801924272,0 38.99573536493773,-11.04178032476444,0 38.99560510673301,-11.04141489855327,0 38.99510243252743,-11.04112962517748,0 38.99408253760913,-11.04146530113384,0

bushland2:39.00340243948988,-11.04234996851613,0 39.00296537982829,-11.04267663255115,0 39.00290506714792,-11.04270636631092,0 39.00271044958266,-11.04355103802362,0 39.00271058813281,-11.04362510127527,0 39.00308922316352,-11.04433543402553,0 39.0031345553759,-11.04436497858972,0 39.00316485086498,-11.04442417551431,0 39.00373863444808,-11.04457127447502,0 39.00378391140981,-11.04457119324793,0 39.00387446515641,-11.04457103079692,0 39.00390464975507,-11.04457097664739,0 39.00399520383272,-11.04457081420083,0 39.0048249780549,-11.04440638372754,0 39.00488526395355,-11.04436183684724,0 39.00493045753674,-11.04431731675602,0 39.00497556702709,-11.04422835844338,0 39.00532182732001,-11.04376853757042,0 39.00533683604543,-11.04372407191459,0 39.00533666834274,-11.04363519519432,0 39.00533655656339,-11.04357594408248,0 39.00522982073898,-11.04299843699759,0 39.00521461668694,-11.04293921319916,0 39.00519941289599,-11.04287998957356,0 39.00494189599247,-11.04237682073024,0 39.00492669253268,-11.0423175973396,0 39.00477524143122,-11.04203642918745,0 39.00340243948988,-11.04234996851613,0

  • This input data is output from another script, genericGEkml2csv.bash and originally comes from Google Earth (self-digitized polygon kml's).
  • The output shp is in geographic WGS84, but does not carry that information. You can transform it e.g. into UTM 36S WGS84 with the following command:
ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:32736 proj_output.shp output.shp 
  • Where EPSG:4326 stands for WGS84 (source system) and EPSG:32736 for UTM 36S WGS84 (target system). You can select any target system and find the EPSG code, see http://spatialreference.org/ref/epsg/

Exercise:


This script performs conversion from a set of generic .kml format polygons created in Google Earth (GE) into one combined textfile. This textfile can then be converted into a shapefile using script genericCsvToPolygon.py

  • How to create polygons in Google Earth and save them as .kml files, see here
  • Then open your working directory using
cd /home/...

The procedure is:

1. Put the kml's into one folder

2. Launch genericGEkml2csv.bash in that kml-folder. This creates a csv file "output.csv"

genericGEkml2csv.bash

3. Launch genericCsvToPolygon.py in the same folder, with parameters as follows:

genericCsvToPolygon.py output.csv output.shp 

The shapefile name can be as you wish (e.g. settlements168063.shp).

The shapefile is in geographic WGS84, but does not carry that information. You can transform it e.g. into UTM 36S WGS84 with the following command - Input: output.shp; Output: proj_output.shp:

ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:32736 proj_output.shp output.shp 

Where EPSG:4326 stands for WGS84 (source system) and EPSG:32736 for UTM 36S WGS84 (target system). You can select any target system and find the EPSG code, see http://spatialreference.org/ref/epsg/



Back to Open Foris Toolkit Main Page

Back to Tools & Exercises



Personal tools