test_plotg.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from shapely.geometry import LineString, Polygon
  2. from shapely.ops import unary_union
  3. from matplotlib.pyplot import plot, subplot, show, axes
  4. from matplotlib.axes import *
  5. from camlib import *
  6. def plotg2(geo, solid_poly=False, color="black", linestyle='solid'):
  7. try:
  8. for sub_geo in geo:
  9. plotg2(sub_geo, solid_poly=solid_poly, color=color, linestyle=linestyle)
  10. except TypeError:
  11. if type(geo) == Polygon:
  12. if solid_poly:
  13. patch = PolygonPatch(geo,
  14. #facecolor="#BBF268",
  15. facecolor=color,
  16. edgecolor="#006E20",
  17. alpha=0.5,
  18. zorder=2)
  19. ax = subplot(111)
  20. ax.add_patch(patch)
  21. else:
  22. x, y = geo.exterior.coords.xy
  23. plot(x, y, color=color, linestyle=linestyle)
  24. for ints in geo.interiors:
  25. x, y = ints.coords.xy
  26. plot(x, y, color=color, linestyle=linestyle)
  27. if type(geo) == LineString or type(geo) == LinearRing:
  28. x, y = geo.coords.xy
  29. plot(x, y, color=color, linestyle=linestyle)
  30. if type(geo) == Point:
  31. x, y = geo.coords.xy
  32. plot(x, y, 'o')
  33. if __name__ == "__main__":
  34. p = Polygon([[0, 0], [0, 5], [5, 5], [5, 0]])
  35. paths = [
  36. LineString([[0.5, 2], [2, 4.5]]),
  37. LineString([[2, 0.5], [4.5, 2]])
  38. ]
  39. plotg2(p, solid_poly=True)
  40. plotg2(paths, linestyle="dashed")
  41. show()