Kagee

Untitled

Jan 5th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. from osgeo import ogr
  2. from osgeo import osr
  3.  
  4. def h(name):
  5.   print "<Placemark><name>%s</name>" % (name,);
  6.  
  7. def f():
  8.   print "</Placemark>";
  9.  
  10. print """
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <kml xmlns="http://www.opengis.net/kml/2.2">
  13.  <Document>""";
  14.  
  15. gj_oslo = """{"type":"Point","coordinates":[59.95,10.75]}"""
  16. gj_paris = """{"type":"Point","coordinates":[48.8567,2.3508]}"""
  17.  
  18. p_oslo = ogr.CreateGeometryFromJson(gj_oslo)
  19. p_paris = ogr.CreateGeometryFromJson(gj_paris)
  20.  
  21. h("point oslo")
  22. print p_oslo.ExportToKML()
  23. f()
  24. h("point paris")
  25. print p_paris.ExportToKML()
  26. f()
  27.  
  28. # http://geojson.org/geojson-spec.html#coordinate-reference-system-objects
  29. # EPSG:4326
  30. sr_geojson = osr.SpatialReference()
  31. sr_geojson.ImportFromEPSG(4326)
  32. # sr_geojson = p_oslo.GetSpatialReference()
  33.  
  34. # Oslo: 32V (31N: EPSG:32632)
  35. sr_oslo = osr.SpatialReference()
  36. sr_oslo.ImportFromEPSG(32632)
  37.  
  38. # Paris: 31U (31N: EPSG:32631)
  39. sr_paris = osr.SpatialReference()
  40. sr_paris.ImportFromEPSG(32631)
  41.  
  42. tr_geo_oslo = osr.CoordinateTransformation(sr_geojson, sr_oslo)
  43. tr_geo_paris = osr.CoordinateTransformation(sr_geojson, sr_paris)
  44.  
  45. tr_oslo_geo = osr.CoordinateTransformation(sr_oslo, sr_geojson)
  46. tr_paris_geo = osr.CoordinateTransformation(sr_paris, sr_geojson)
  47.  
  48. # If i understand correctly, UTM has a unit of 1 metre
  49. p_oslo.Transform(tr_geo_oslo)
  50. p_paris.Transform(tr_geo_paris)
  51.  
  52. # Buffer requires the radius to be in the same unit as the coordinates
  53. poly_oslo = p_oslo.Buffer(600 * 1000) # 600 km
  54. poly_paris = p_paris.Buffer(1200 * 1000) # 1200 km
  55.  
  56. # Transform back before we do the intersection
  57. poly_oslo.Transform(tr_oslo_geo)
  58. poly_paris.Transform(tr_paris_geo)
  59.  
  60. intersection = poly_oslo.Intersection(poly_paris)
  61.  
  62. #poly_oslo.Transform(tr_oslo_geo)
  63. #poly_paris.Transform(tr_paris_geo)
  64.  
  65. h("poly oslo")
  66. print poly_oslo.ExportToKML()
  67. f()
  68. h("poly paris")
  69. print poly_paris.ExportToKML()
  70. f()
  71.  
  72. intersection.Transform(tr_oslo_geo)
  73.  
  74. h("poly intersect")
  75. # Why are you empty????
  76. print intersection.ExportToKML()
  77. f()
  78.  
  79. print """</Document>
  80. </kml>"""
Advertisement
Add Comment
Please, Sign In to add comment