fitodic / centerline

Calculate the polygon's centerline
https://centerline.readthedocs.io
MIT License
266 stars 55 forks source link

Add support for Shapely 2.0 #30

Closed fitodic closed 1 year ago

fitodic commented 2 years ago

Shapely will drop support for setting custom attributes starting from version 2.0. The geometry would be made available via the Centerline.geometry attribute instead.

Fixes: https://github.com/fitodic/centerline/issues/29 https://github.com/fitodic/centerline/issues/35 https://github.com/fitodic/centerline/issues/36

mrazzaq19 commented 1 year ago

Hello there, Following your guideline to run the python: from shapely.geometry import Polygon from centerline.geometry import Centerline

polygon = Polygon([[0, 0], [0, 4], [4, 4], [4, 0]]) attributes = {"id": 1, "name": "polygon", "valid": True}

centerline = Centerline(polygon, **attributes) centerline.id == 1 True centerline.name 'polygon' centerline.geoms <shapely.geometry.base.GeometrySequence object at 0x7f7d24116210>

How do we visualize the output?? and how to read the polygone.shp file??

fitodic commented 1 year ago

Hello @mrazzaq19,

to achieve that, you'll have to use other 3rd party libraries. For visualization, you can take a look at matplotlib, whereas to read a shapefile, you can take a look at fiona or at the create_centerlines command.

mrazzaq19 commented 1 year ago

Dear Filip, First of all thanks a lot for the prompt reply, appreciated. I have done with reading file.shp and visualizing it.

  1. How can one load and see the vironoi diagram figure you shared on (https://centerline.readthedocs.io/en/latest/)?

  2. Command-line interface: How does that work?

  3. Python: For this, I got this out <shapely.geometry.base.GeometrySequence at 0x7f918f448a50> How do I save and visualize this?

This must be a toy problem for you but means a lot

fitodic commented 1 year ago

How can one load and see the vironoi diagram figure you shared on

You can take a look at QGIS. I used that to visualize the diagrams by importing the shapefile.

Command-line interface: How does that work?

It's a terminal that works on Windows and Linux. You open it, activate the environment the package is installed in and run the create_centerlines command:

create_centerlines /path/to/your/input.shp /path/to/your/output.shp

That command will take in all the areas your shapefile has, create centerlines and save them into the output shapefile.

Python: For this, I got this out... How do I save and visualize this?

It's what the create_centerlines does for you, but basically you'd have to open a new file and write the centerline data to it:


```with fiona.open(
    dst,
    mode="w",
    driver=driver.GetName(),
    schema=schema,
    crs=source_file.crs,
    encoding=source_file.encoding,
) as destination_file:
    for record in source_file:
        geom = record.get("geometry")
        input_geom = shape(geom)

        attributes = record.get("properties")
        try:
            centerline_obj = Centerline(input_geom, interpolation_distance, **attributes)
        except (InvalidInputTypeError, TooFewRidgesError) as error:
            logging.warning(error)
            continue

        centerline_dict = {
            "geometry": mapping(centerline_obj),
            "properties": {
                k: v
                for k, v in centerline_obj.__dict__.items()
                if k in attributes.keys()
            },
        }

        destination_file.write(centerline_dict)
mrazzaq19 commented 1 year ago

Thanks, create_centerlines /path/to/your/input.shp /path/to/your/output.shp works now and the zipped shp folder is visualized to see the centerlines in QGIS.

Many thanks for your guidance and prompt responses.

fitodic commented 1 year ago

Writing the centerline data: I didn't get that! how to create after saving? is it through QGIS or command line command??

It really depends on your use-case. If you all your data in a shapefile or some other format, than the create_centerlines command should be enough. If you want to customize a part of the process, you'll need to load the areas, create Centerline objects and save them by opening a file and writing to it.

Is it possible to construct a centerline: considering the inner rectangle as a hole?

Yes, the Centerline class handles those cases as well. There's nothing special about them, but you may end up with certain spruces that stick out (depending on the level of detail) that you may want to delete manually using QGIS.

mrazzaq19 commented 1 year ago

can we import and convert vector files to shape files? for instances: ps to shp? and can we construct multipolygones based on vector data e.g. 'polygon1 = Polygon([[0,0] , [16, 0], [16, 10], [0, 10], [4, 3], [4, 7], [8, 7], [8, 3] ])' ??

fitodic commented 1 year ago

Yes, if the hole is an area, it'll get its own centerline that can be manually removed using QGIS.

The create_centerlines command should support all vector drivers provided by OGR, but I only used shapefiles and geojson until now.

phamkhactu commented 1 year ago

Hi @fitodic Thanks for great repo I followed the tutorial to get centerline but get error:

TypeError: __new__() got an unexpected keyword argument 'id'

My version is:

Shapely:2.0
Centerline:0.6.4

Here my data:

d = { "type": "Feature", "properties": { "OBJECTID": 6.0, "SHAPE_Leng": 0.0018435614763720001, "SHAPE_Area": 1.830693e-08, "name": None, "rongMax": None, "rongMin": None, "rongTB": None }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 105.814233473000058, 21.009605263000026 ], [ 105.814270254000064, 21.009557108000024 ], [ 105.814294123000082, 21.009508220000043 ], [ 105.814310222000074, 21.00947741400006 ], [ 105.814272326000037, 21.009456668000041 ], [ 105.814212241000064, 21.009415109000031 ], [ 105.81420315400004, 21.00940840100003 ], [ 105.81417328200007, 21.009386347000031 ], [ 105.814165209000066, 21.009398109000074 ], [ 105.814144615000032, 21.009394712000073 ], [ 105.814124802000038, 21.009382587000061 ], [ 105.814117689000057, 21.009392403000049 ], [ 105.814070315000038, 21.009362398000064 ], [ 105.814035767000064, 21.00933989300006 ], [ 105.81399544900006, 21.009314226000072 ], [ 105.813992317000043, 21.00931666200006 ], [ 105.813938111000084, 21.009278877000042 ], [ 105.813956262000033, 21.009253924000063 ], [ 105.813898282000082, 21.009273615000041 ], [ 105.813906806000034, 21.009281664000071 ], [ 105.813926242000036, 21.009296736000067 ], [ 105.813930008000057, 21.009292257000027 ], [ 105.814022481000052, 21.009355528000071 ], [ 105.814091046000044, 21.009398283000053 ], [ 105.814108600000054, 21.009407042000078 ], [ 105.814147095000067, 21.009413957000049 ], [ 105.814147350000042, 21.009413089000077 ], [ 105.814174914000034, 21.009421213000053 ], [ 105.814174412000057, 21.009422525000048 ], [ 105.814199988000041, 21.009438416000023 ], [ 105.814196086000038, 21.00944436900005 ], [ 105.81427995100006, 21.009495137000044 ], [ 105.81422490500006, 21.009573164000074 ], [ 105.814231047000078, 21.009576607000042 ], [ 105.814197398000033, 21.009632903000067 ], [ 105.814200504000041, 21.009634568000024 ], [ 105.814165204000062, 21.009698650000075 ], [ 105.814130529000067, 21.009681203000071 ], [ 105.814110726000081, 21.009670929000038 ], [ 105.814077452000049, 21.00965476600004 ], [ 105.814043983000033, 21.009639770000035 ], [ 105.814037492000068, 21.009636667000052 ], [ 105.814028830000041, 21.009653373000049 ], [ 105.814056730000061, 21.009666333000041 ], [ 105.814085146000082, 21.009679532000064 ], [ 105.814120301000059, 21.009698368000045 ], [ 105.814155253000081, 21.009715444000051 ], [ 105.81417351500005, 21.009716377000075 ], [ 105.814180653000051, 21.009702379000032 ], [ 105.814231116000087, 21.009606946000076 ], [ 105.814233473000058, 21.009605263000026 ] ] ] ] } }

I am very happy if you can give your code example to get centerline from polygons(my data) Thank you

codecov[bot] commented 1 year ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@af7670e). Click here to learn what that means. Patch has no changes to coverable lines.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #30 +/- ## ========================================== Coverage ? 100.00% ========================================== Files ? 4 Lines ? 148 Branches ? 20 ========================================== Hits ? 148 Misses ? 0 Partials ? 0 ``` Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Filip+Todic). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Filip+Todic)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.