Select to view content in your preferred language

Why Don't Some Properties Have Setters?

669
3
08-08-2022 06:39 AM
AnthonyWalsh3
Occasional Contributor

Is there a reason why I cannot set X, Y, Z values of a MapPoint? When I want to update, say, the latitude of the point, I have to create a new MapPoint object, I cannot just change the Y value because the property has only getter, no setter. 

There is a similar problem with most of the other classes too. For example I cannot add a new point to the polyline to re-render it, I have to re-create it. 

Why is this the case, is there a specific reason for this architecture?

0 Kudos
3 Replies
Nicholas-Furness
Esri Regular Contributor

In short, performance. Based off lessons learnt in the previous generation Runtime SDKs, we decided that geometries should be immutable. This way we can be explicit about monitoring and reflecting updates. For example, if you re-use a point in two places, what does that mean if you modify it? If that point is part of a polygon or polyline, modifying it could change the valid or "simple" state of that polygon/polyline, which can have various repercussions on rendering paths. And of course there's a performance impact in monitoring and propagating that change.

Instead, you use Geometry Builders. These are lightweight objects that can accept an existing geometry or start from a blank slate, allow manipulation and construction of a new geometry, and then output that new geometry.

For example, to add a point to a polyline, you can create a PolylineBuilder from the polyline, add a point, then call ToGeometry() to get a modified polyline.

To modify a MapPoint, create a MapPointBuilder from the MapPoint, modify the X/Y/Z/M properties as needed using the getters and setters, then call ToGeometry() to get an updated MapPoint.

Depending on what you want to do, there are also methods on GeometryEngine that could come in handy.

AnthonyWalsh3
Occasional Contributor

Thanks for the answer. I will examine the Geometry Builder classes. However for the polyline example, there is addpoint method for the PolylineBuilder, but I still cannot find a way to modify a single point or remove a point from a polyline. Am I missing something?

For instance, is there a way to move a mappoint in the polyline to another location, while the rest of the points remain untouched? Similarly how to remove some of the points and insert points  in specific indices? PolylineBuilder only provides addpoint/s methods...

0 Kudos
Nicholas-Furness
Esri Regular Contributor

Look at the Parts collection on the PolylineBuilder.

Each Part represents a sequence of joined MapPoints along the Polyline. For a simple Polyline with no breaks, there could well just be 1 Part. Each Part is comprised of a sequence of MapPoints, and Segments between them (Segments could be straight lines, cubic beziers, or elliptic arcs). You can Add (append), Remove, and Insert MapPoints (and Segments for that matter) on the Part. You can also update a point by calling SetPoint().

Everything you want to modify on a Polyline should be doable on PolylineBuilder (even if you need to dig in to the Parts). The key is the split between the immutable geometry, and the modifiable builder.