Skip to content

How to calculate area of closed contour #1139

Answered by gswifort
dizzysan84 asked this question in Q&A
Discussion options

You must be logged in to vote

You can also do it simpler :)

import ezdxf
import ezdxf.entities
import shapely


def get_lines(ents):
    for ent in ents:
        if isinstance(ent, ezdxf.entities.Line):
            yield tuple(ent.dxf.start)[:2], tuple(ent.dxf.end)[:2]
        elif isinstance(ent, ezdxf.entities.LWPolyline):
            yield tuple(ent.vertices())


def main():
    doc = ezdxf.readfile(r"...")
    msp = doc.modelspace()
    ents = msp.query("LINE LWPOLYLINE")

    ls = shapely.MultiLineString(tuple(get_lines(ents)))
    lm = shapely.line_merge(ls)
    polygon = shapely.Polygon(lm)

    print(f"Area: {polygon.area}")


if __name__ == "__main__":
    main()

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@gswifort
Comment options

Answer selected by mozman
@dizzysan84
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants