Select to view content in your preferred language

ArcGIS error "target_extent has zero for width or height"

1387
6
Jump to solution
10-03-2022 08:52 PM
trieunguyenaxon
Occasional Contributor

I tried to set view point for ArcGIS map and encountered an error exception:

ArcGIS Runtime Error Occurred. Set a breakpoint on C++ exceptions to see the original callstack and context for this error:  Error Domain=com.esri.arcgis.runtime.error Code=2 "Invalid argument." UserInfo={NSLocalizedFailureReason=target_extent has zero for width or height., NSLocalizedDescription=Invalid argument., Additional Message=target_extent has zero for width or height.}

The code to set the view point is attached bellow, and I recognized that the error just happens with some AGSPoint.

0 Kudos
1 Solution

Accepted Solutions
Ting
by Esri Contributor
Esri Contributor

Well, a possible workaround might look like this

let extent: AGSEnvelope
if min(polygon.extent.width, polygon.extent.height) == .zero {
    let length = max(polygon.extent.width, polygon.extent.height)
    extent = AGSEnvelope(center: polygon.extent.center, width: length, height: length)
} else {
    extent = polygon.extent
}
mapView.setViewpointGeometry(extent)

When the extent of the polygon/polyline has zero width or height, add a condition to adjust its extent to a square, so that it can be correctly converted to a valid viewpoint target extent.

 

If you don't mind losing the original tightly-bound behavior, simply create a square extent for all geometries, regardless of whether it has a zero dimension or not.

let length = max(polygon.extent.width, polygon.extent.height)
let extent = AGSEnvelope(center: polygon.extent.center, width: length, height: length)
mapView.setViewpointGeometry(extent)

 

Although this is just a temporary fix and doesn't work perfectly. Hopefully the bug will be addressed in the upcoming versions of Runtime. 🙂

View solution in original post

6 Replies
Ting
by Esri Contributor
Esri Contributor

Please see the attached project. I cannot reproduce the error.

Can you give another try or provide a minimal reproducible example?

One thing to consider, although it works, a "polygon" with only 2 vertices is basically a polyline.

trieunguyenaxon
Occasional Contributor

I send you the reproduce project. I already updated the locations which caused this issue.
Could you help me take a look? Thank you so much

0 Kudos
Ting
by Esri Contributor
Esri Contributor

I see. It happens when the points are sharing the same latitude or longitude. I can see why it might happen - if all the points are on a same line parallel to x or y axis, then the bounding box (extent) of the geometry would be ill-formed, not being a rectangle but a line.

I'll query our team on their thoughts, but I personally think the behavior is expected, as indicated in the error message: target_extent has zero for width or height. A point or a x/y-parallel polyline doesn't have a valid rectangle extent.

Edit: team think it is a bug for not working on horizontal and vertical lines, will be fixed in the future.

 

In the meantime, here are a few alternatives you may consider.

  • Setting viewpoint with center and scale.

You can always set a viewpoint to the center of your geometry and apply a scale, such as

 

mapView.setViewpointCenter(polygon.extent.center, scale: 1e4)

this is preferred because you can control the exact location and scale where you want to look at. Consider a point could represent a street address, or the centroid of a country - how much you zoom depends on what the point represents

 

  • Setting viewpoint with a buffered geometry.

If you buffer the center point of a geometry to a circle, it would always have a valid extent. Please notice, the planar buffer works the best with an equal-area projection map, and makes more sense to use with Web Mercator than WGS 84. We do have a geodetic buffer but that might be an overkill here.

 

// Note the distance is in the unit of the geometry's spatial reference, so with lat-lon it means 1 degree.
mapView.setViewpointGeometry(AGSGeometryEngine.bufferGeometry(polygon.extent.center, byDistance: 1)!)

 

trieunguyenaxon
Occasional Contributor

Grate! This solution works for me. But with the first solution, could you share with me how to calculate the scale factor. Because the scale is depended on the points which I want to show on the map. I want to show all of the points to the map, so the scale should be suitable to show all points.

mapView.setViewpointCenter(polygon.extent.center, scale: 1e4)

 

0 Kudos
Ting
by Esri Contributor
Esri Contributor

Well, a possible workaround might look like this

let extent: AGSEnvelope
if min(polygon.extent.width, polygon.extent.height) == .zero {
    let length = max(polygon.extent.width, polygon.extent.height)
    extent = AGSEnvelope(center: polygon.extent.center, width: length, height: length)
} else {
    extent = polygon.extent
}
mapView.setViewpointGeometry(extent)

When the extent of the polygon/polyline has zero width or height, add a condition to adjust its extent to a square, so that it can be correctly converted to a valid viewpoint target extent.

 

If you don't mind losing the original tightly-bound behavior, simply create a square extent for all geometries, regardless of whether it has a zero dimension or not.

let length = max(polygon.extent.width, polygon.extent.height)
let extent = AGSEnvelope(center: polygon.extent.center, width: length, height: length)
mapView.setViewpointGeometry(extent)

 

Although this is just a temporary fix and doesn't work perfectly. Hopefully the bug will be addressed in the upcoming versions of Runtime. 🙂

trieunguyenaxon
Occasional Contributor

Thank you for your solution. It work perfectly 😎

0 Kudos