Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. from shapely.geometry import Point, mapping, shape
  2. from fiona import collection
  3. #from shapely.ops import intersection
  4. import shapely.ops
  5.  
  6. bufSHP = 'data/h1_buf.shp'
  7. intSHP = 'data/h1_buf_int_ct.shp'
  8. ctSHP = 'data/nyct2010.shp'
  9. with collection(bufSHP, "r") as input:
  10. schema = input.schema.copy()
  11. with collection(intSHP, "w", "ESRI Shapefile", schema) as output:
  12. shapes = []
  13. for f in input:
  14. shapes.append(shape(f['geometry']))
  15. merged = shapes.intersection(ctSHP)
  16. output.write({
  17. 'properties': {
  18. 'uid': point['properties']['uid']
  19. },
  20. 'geometry': mapping(merged)
  21. })
  22.  
  23. import fiona
  24. from shapely.geometry import shape, mapping
  25. import rtree
  26.  
  27. bufSHP = 'data/h1_buf.shp'
  28. intSHP = 'data/h1_buf_int_ct.shp'
  29. ctSHP = 'data/nyct2010.shp'
  30.  
  31. with fiona.open(bufSHP, 'r') as layer1:
  32. with fiona.open(ctSHP, 'r') as layer2:
  33. # We copy schema and add the new property for the new resulting shp
  34. schema = layer2.schema.copy()
  35. schema['properties']['uid'] = 'int:10'
  36. # We open a first empty shp to write new content from both others shp
  37. with fiona.open(intSHP, 'w', 'ESRI Shapefile', schema) as layer3:
  38. index = rtree.index.Index()
  39. for feat1 in layer1:
  40. fid = int(feat1['id'])
  41. geom1 = shape(feat1['geometry'])
  42. index.insert(fid, geom1.bounds)
  43.  
  44. for feat2 in layer2:
  45. geom2 = shape(feat2['geometry'])
  46. for fid in list(index.intersection(geom2.bounds)):
  47. if fid != int(feat2['id']):
  48. feat1 = layer1[fid]
  49. geom1 = shape(feat1['geometry'])
  50. if geom1.intersects(geom2):
  51. # We take attributes from ctSHP
  52. props = feat2['properties']
  53. # Then append the uid attribute we want from the other shp
  54. props['uid'] = feat1['properties']['uid']
  55. # Add the content to the right schema in the new shp
  56. layer3.write({
  57. 'properties': props,
  58. 'geometry': mapping(geom1.intersection(geom2))
  59. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement