fzxzmap.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import arcpy
  4. import uuid
  5. import arcpy
  6. class FzxzMap:
  7. def __init__(self):
  8. self.root = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../mxds/fzxz"))
  9. self.aprx = os.path.join(self.root, r"FzxzMap.aprx")
  10. self.shp = os.path.join(self.root, r"fzxz.shp")
  11. # 获取图层的范围
  12. def get_layer_extent(self, layer):
  13. desc = arcpy.Describe(layer)
  14. if desc and desc.extent:
  15. return desc.extent
  16. return None
  17. def run(self):
  18. aprx = arcpy.mp.ArcGISProject(self.aprx)
  19. map_doc = aprx.listMaps()[0]
  20. layF = map_doc.listLayers('fzxzF')[0]
  21. layM = map_doc.listLayers('fzxzM')[0]
  22. jpgPath = ""
  23. cursor = arcpy.da.SearchCursor(self.shp, ['BSM', 'RWBSM'])
  24. for row in cursor:
  25. bsm = row[0]
  26. rwbsm = row[1]
  27. if jpgPath == "":
  28. jpgPath = os.path.join(self.root, "jpgs", str(rwbsm))
  29. if not os.path.exists(jpgPath):
  30. os.makedirs(jpgPath)
  31. where = "BSM='{}'".format(str(bsm))
  32. # print(where)
  33. if layF.supports('DefinitionQuery'):
  34. dql = layF.listDefinitionQueries()
  35. # dql = []
  36. for dq in dql:
  37. dq['isActive'] = False
  38. dql.append({'name': bsm, 'sql': where, 'isActive': True})
  39. layF.updateDefinitionQueries(dql)
  40. # aprx.save()
  41. # 全局
  42. fileF = os.path.join(jpgPath, '{0}_F.jpg'.format(bsm))
  43. if not os.path.exists(fileF):
  44. if layF:
  45. local_extent = self.get_layer_extent(layF)
  46. # print(local_extent)
  47. map_doc.defaultCamera.setExtent(local_extent)
  48. # aprx.save()
  49. map_doc.defaultView.exportToJPEG(fileF, width=400, height=240, resolution=300)
  50. print(fileF.replace("Z:", "").replace("\\", "/"))
  51. # 局部
  52. fileM = os.path.join(jpgPath, '{0}_M.jpg'.format(bsm))
  53. if not os.path.exists(fileM):
  54. if layM:
  55. temp_layer = r"in_memory\selected_layer" + bsm
  56. arcpy.analysis.Select(layF, temp_layer, where)
  57. # 获取临时选择图层的范围
  58. temp_layer_extent = self.get_layer_extent(temp_layer)
  59. map_doc.defaultCamera.setExtent(temp_layer_extent)
  60. # aprx.save()
  61. map_doc.defaultView.exportToJPEG(fileM, width=400, height=240, resolution=300)
  62. print(fileM.replace("Z:", "").replace("\\", "/"))
  63. # map = FzxzMap()
  64. # map.run()