Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. # translate_multipoint
  2.  
  3. # Translation of Shapefile record geometries in a functional style.
  4.  
  5. from fiona import collection
  6. from functools import partial
  7. from itertools import imap
  8. import logging
  9.  
  10. log = logging.getLogger()
  11.  
  12. # To begin: a few functions to translate geometry coordinates. They call each
  13. # other semi-recursively. GeoJSON was designed to make this possible, BTW.
  14.  
  15. def translate_Point(coords, delta):
  16. """Returns translated Point coordinates, preserving order, number,
  17. and dimensionality.
  18.  
  19. delta is a (delta_x, delta_y [, delta_y]) tuple."""
  20. print 'coords', coords
  21. print 'delta', delta
  22. result = [tuple(c + d for c, d in zip(coords[0], delta))]
  23. print 'result', result
  24. return result
  25.  
  26. def translate_LineString(coords, delta):
  27. """Returns translated LineString or Ring coordinates, preserving
  28. order, number, and dimensionality.
  29.  
  30. delta is a (delta_x, delta_y [, delta_y]) tuple."""
  31. # Calls translate_Point.
  32. return list(translate_Point(pt_coords, delta) for pt_coords in coords)
  33.  
  34. def translate(delta, rec):
  35. print "translate"
  36. """Returns a record after translating its geometry's coordinates.
  37.  
  38. delta is a (delta_x, delta_y [, delta_y]) tuple."""
  39.  
  40. # Use lexical dispatch on geometry type to get the proper translation
  41. # function.
  42. handlers = {
  43. 'MultiPoint': translate_Point}
  44. try:
  45. g = rec['geometry']
  46. print 'g', g
  47. g['coordinates'] = handlers[g['type']](g['coordinates'], delta )
  48. rec['geometry'] = g
  49. return rec
  50. except Exception, e:
  51. log.exception("Error processing record %s:", rec)
  52.  
  53. # And now the processing code. First, open a source file.
  54. with collection("D:\\shawbost_friday_osgb_1936.shp", "r") as source:
  55.  
  56. # Create a sink file for processed features with the same format and
  57. # coordinate reference system as the source.
  58. with collection(
  59. "translated_osgb_1936_dalmore2.shp",
  60. "w",
  61. driver=source.driver,
  62. schema=source.schema,
  63. crs=source.crs
  64. ) as sink:
  65.  
  66. # Example 2D translation, 1 unit eastward and northward.
  67. results = imap(partial(translate, (-4153.8348, -2604.8443)), source)
  68. sink.writerecords(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement