You are here: > ESRI Forums > arcview 3.x users discussion forums > Thread Replies

ArcView 3.x Users Discussion Forums

ArcView GIS - Core program forum

Polygon width and length calculation   Tracy Chan Aug 21, 2002
Re: Polygon width and length calculation   David Kimball Aug 21, 2002
Re: Polygon width and length calculation   Tracy Chan Aug 22, 2002
Re: Polygon width and length calculation   David Kimball Aug 22, 2002
Re: Polygon width and length calculation   William Huber Aug 22, 2002
Re: Polygon width and length calculation   William Huber Aug 23, 2002
Re: Polygon width and length calculation   Tracy Chan Aug 30, 2002
Re: Polygon width and length calculation   Pieter Geert van den Beukel Aug 30, 2002
Re: Polygon width and length calculation   William Huber Aug 30, 2002
Re: Polygon width and length calculation   Tracy Chan Aug 30, 2002
Re: Polygon width and length calculation   William Huber Aug 30, 2002
Re: Polygon width and length calculation   Tracy Chan Sep 04, 2002
Re: Polygon width and length calculation   William Huber Sep 04, 2002
Re: Polygon width and length calculation   William Huber Sep 03, 2002
Re: Polygon width and length calculation   Kirsten Lawrence Sep 05, 2007
Re: Polygon width and length calculation   Dan Patterson Sep 05, 2007
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Polygon width and length calculation 
Author Tracy Chan 
Date Aug 21, 2002 
Message I have a polygon shapefile of ~60,000 street roadbeds which I am trying to derive the width from (length too but width is most important.) Although many are simple rectangles, there are also many irregular shape polygons and I don't expect to be able to get a width result for all of them. Ideally I would like to find the Minimum, Maximum and Average width of the polygon though any of these measurements would be helpful. Does anyone have any suggestions for how to do this in ArcView 3.x or ArcGIS 8.x? Is there any function in the software (including using programming script) to calculate the width automatically?

Sample polygons can be viewed/downloaded at: http://www.geo.hunter.cuny.edu/~schan/Sample/Sample.htm

Thank you and any help will be appreciate.
Tracy 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author David Kimball 
Date Aug 21, 2002 
Message Are you trying to calculate the width of the roads? You'll find that "width" is a very slippery concept. Without going into too much detail, I don't think there is any simple way to calculate what a normal person would think of as the width of a road polygon, especially looking at the irregular polygons in your sample image.

One approach I can think of would work only if you also have a road centerline line theme. You could turn your polygons into lines and calculate the minimum distance from a bunch of points along a centerline to the corresponding roadbed line. The median of the distances from enough points might give you (when multiplied by 2) a typical road width for that road (although this'd be less reliable for a polygon like #8481). This would require a bit of programming though, and you'd need an accurate centerline.

There also might be some creative raster solution to this, something like making each road into a GRID and calculating a distance GRID from the centerline and analyzing the distribution of distances present in the distance GRID...this would be a bit too complex though, I reckon.

Good luck.
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Tracy Chan 
Date Aug 22, 2002 
Message The polygons in fact are associated with the NYC Lion Street file, which in many cases can be considered as the centerlines of polygons. (I put another images on the web page that includes the street segment on the polygon).
I don't really understand how to derive the width from let say a line. Can you please tell me more detail about it?
Also, if I convert the polygon into raster image, what specific function can I use to do the calculation? Thanks a lot.

I also tried use the script [shape].ReturnExtent.GetWidth. I got a new value but I didn't know what those numbers represent. I measured the width and length of some polygons manually, however, the measurement did not match with the width value. Anyone knows anything about that width value? Thanks 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author David Kimball 
Date Aug 22, 2002 
Message The aPolygon.ReturnExtent.GetWidth value is the width of the polygon's "extent" or bounding box (a rectangle that is oriented straight up-and-down and is the smallest such rectangle that can completely enclose the polygon). This will almost never be the value you are looking for.

As far as the suggestions I made, the raster one would require someone really clever to make it work, and the vector (line) one would be slightly less difficult but still would require quite a bit of Avenue programming and I'm not entirely sure that it'd work. Basically, they were just vague ideas I thought up. I unfortunately don't have time to help you by writing a complex script!

In case you know some Avenue or know someone who does, you could try:

1) getting a polyline object that is the boundary of the polygon (using Polyline.Make({aPolygon.AsPolyline.AsList.Get(0)})

2) getting the centerline polyline that corresponds to that polygon (you will need to clip it to the polygon if it continues outside the polygon)

3) make a list of points that are a certain distance along the centerline (i.e. 20% along, 30% along, ... 80% along) using aPolyline.Along(20) etc.

4) calculate the distance from each point to the polyline (the polygon-derived one).

5) the hard part would be deciding which of these values best represented the street's width. Unfortunately the results would be imperfect at best.

Sorry I don't have a real answer for you! Maybe someone else has tackled this problem before and has a better insight.



One last thought: another approach could be to do something with the ratio of the polygon's area to its perimeter. This wouldn't be 100% accurate either, but it'd be easier--you could do it with the field calculator (see below). This would give you a number that wouldn't be the width but might be proportional to the width (a larger number would indicate a larger width RELATIVE TO the length of the corresponding centerline).

This is probably all way too complicated... 
 
[myfield] = 

[shape].returnarea / [shape].returnlength
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Aug 22, 2002 
Message David is right about the "slippery concept" of width. There are, however, some precise definitions that might apply. Two relevant ones are:

(1) Print the polygon on infinitely thin rigid material. Try feeding the polygon through a perfectly circular hole in another piece of infinitely thin rigid material. The diameter of the smallest hole through which the polygon can entirely pass is its "width."

(2) Assume the polygon is connected. Find the least negative buffer width that makes the polygon disappear. Minus two times this buffer width is the polygon's "width."

Techniques exist for tackling (1), but I am not aware of any implementations, especially not in GIS. (The question is of great interest in robotics, where the polygon represents the shape of an object that must be navigated through a cluttered area.)

The value of (2) can be calculated to high accuracy using an efficient root finding technique. (The idea appeared in ArcUser Jan-Mar 1999, p. 41, but you will not find an efficient implementation there.) For any non-negative number x, define f(x) to be the area of the buffer of size -x around the polygon. You just need to find the smallest x for which f(x) = 0. (Technically, it's better to find the x for which f(x) = e where e is a very tiny positive value.) Think of the polygon "evaporating" at a uniform rate from its boundary until nothing is left, almost like Lewis Carroll's Cheshire Cat vanishes from the outside in until only its smile remains. At the moment just before the polygon vanishes, it will consist of a collection of points and lines that are the "most interior." The distance from these to the polygon's original boundary is half its width.

This interior buffering is fairly easy to implement in any GIS, but the calculations do take some time--maybe a tenth of a second to solve it for each of your polygons.

(2) can also be calculated using a generalization of the Voronoi (Thiessen) polygons idea, because the "most interior" points must lie along edges of the Voronoi diagram corresponding to the polygon's original boundary. However, neither ArcView nor any publicly available extension performs this computation. (It can be approximated using the Euclidean distance operation on a grid in Spatial Analyst, but this would have to be done separately for each polygon: a time-consuming technique.)

Length is perhaps even trickier to define and compute. Since many of your polygons appear to have uniform widths--almost as if they were portions of buffers around the centerlines--then using area/width as a surrogate for length may be a quick and reasonable solution, as Kimball suggested.

Perhaps a more accurate solution, easy to implement, would be to evaluate the area of an interior buffer of the polygon for a small buffer distance e. The difference in areas, divided by 2*e, estimates the length plus a multiple of the width. The multiple is approximately equal to (4 - K)/2, where we think of the polygon as representing a thickened polyline that has K free ends. (This assumes your polygons have no "holes" in them.)

For example, consider a skinny rectangle of length l and width w. It intuitively has two "ends". The area of an interior buffer of distance e < w is exactly

Area(-e) = (l-2e) * (w-2e) = l*w + 2*(l+w)*(-e) + 4*(-e)^2,

so Area(0) - Area(-e) = l + w + something really tiny = l + (4-2)/2 * w, just as predicted. ("Area(0)" is, of course, just the polygon's area.) Thus, having computed the width w, you could estimate the length very accurately by choosing a small value of e (say, e = w/100) and computing

[Area(0) - Area(-e)]/2e - w*(4-K)/2

In general, I don't know how you would compute K, but maybe using a value of K=2 throughout would work well for your particular shapes.

A sample implementation of (2) for computing the width is attached.

Thanks to David Kimball for bringing this thread to my attention.
 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Aug 23, 2002 
Message I have been unable successfully to upload attachments using the Web interface for this forum. Therefore I have made the attachment intended for my previous message available at http://www.quantdec.com/software/skeleton/skeleton3.apr .

(It is 11 Kb. If your browser opens the file directly, try saving it as a text file. Alternatively, in IE, right-click on the link and choose Save Target As...) 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Tracy Chan 
Date Aug 30, 2002 
Message Can anyone helps me on Avenue? I was trying to run the scripts provided by Mr. Huber. I was unable to do that. Since I am not familar with Avenue, I don't know how to fix the errors. Can anyone tells me what should I add to the scripts and be able to run them? Thanks and greatly appreciate.
-Tracy Chan 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Pieter Geert van den Beukel 
Date Aug 30, 2002 
Message Add the polygontheme to a view, make the theme active and run the script View.ActiveThemeSkeleton.
Note: the view needs to be the last active document before running the script. So open the script and the view, click on the view to make it active, click on the script to make it active and hit the run button. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Aug 30, 2002 
Message Thanks, Pieter!

By the way, as far as I know there are not any errors in the "skeleton" scripts I made available; but if anyone tries them out and believes there are, I would appreciate being informed (whuber-at-quantdec.com).

In earlier versions of ArcView (3.0 through 3.1) I was able to find errors that derive from underlying bugs in the Avenue geometry code, but I have not been able to reproduce those problems in later versions of ArcView. 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Tracy Chan 
Date Aug 30, 2002 
Message Thank you very much for everyone's help.

I tried what Pieter said but I still got a message said "Wrong class for parameter 1 of request +. Got a(n) Nil, expected a(n) String"
It seems the program stops me at the very last statement "MsgBox.Report("Skeleton Ratios" + NL + theMsg, strTitle)
" in the script View.ActiveThemeSkeleton. I check the syntax of MsgBox.Report that it should be two strings inside. I don't know is it correct. Do I need to do some other steps before to run the script? Or should I make any change of the script or ? Thank you.

- Tracy Chan 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Aug 30, 2002 
Message It is a bug in the script. It occurs when no features are selected at all. This script requires you explicitly to select the features for which you want a skeleton. (Read the "Purpose" header in the script.) Select at least one feature and try again.

BTW, this script is not intended for production work: it was developed to illustrate the process. A production script would probably output the results to a shapefile rather than as a long message plus a bunch of graphics in the view. (The required modification is a simple one--but you don't have to do it, as I will explain.)

Besides, you still have to post-process the results to obtain the widths you are seeking: you need to compute the distances from the skeletons to the boundaries of the original polygons. This is easily done in the field calculator. Create a numeric field and calculate it using the expression

av.run("Polygon.ReturnSkeleton", {[shape], 0.000001, 0.00000001}).Get(1)*2

This avoids using the crude "View.ActiveThemeSkeleton" script altogether.

Even this approach will fail if you have a null input shape or in the rare case the script encounters a bug in ArcView's "geometry engine." The code shown below, which is a slight modification of the "Polygon.ReturnSkeleton" script you already have, is guaranteed to return values that are usable in the Field Calculator. (To use it, you still need the "NR.ZBRENT" and "Shape.BufferDif" scripts in your project.)

I recommend testing this calculation with just one or a few features selected at first, because it can take a long time. When you are confident everything is working well, select all remaining features and let it fly... 
 
'
' Polygon.ReturnSkeleton
'
' SELF: {
'   thisShape: Polygon
'   r:         Skeleton parameter, between 0 and 1.
'   tolerance: Tolerance factor, positive, close to zero.
' }
'
' Returns: the r-skeleton of thisShape, computed to within
' the given tolerance.  Tolerance is a fraction of the
' polygon perimeter.
' 
' 22 Aug 2002: Also returns the buffer distance.
'
' Requires: Script "NR.ZBRENT", "Shape.BufferDiff"
'
' (c) 1999-2002 Quantitative Decisions.  All rights reserved.
' AV 3.1, 1999
' AV 3.3, 2002
'
' The skeleton parameter is the fraction of the original
' polygon's area to achieve through a negative buffer.
'============================================================' 
thisShape = SELF.Get(0)
r = SELF.Get(1)
Tol = SELF.Get(2)
'
' Obtain the function script for ZBRENT.
'
scrF = av.GetProject.FindScript("Shape.BufferDiff")
if (scrF = NIL) then
  return {thisShape.GetClass.MakeNull, Number.MakeNull}
end ' Could not find the script
'
' Check that this shape has nonempty interior.
'
A = thisShape.ReturnArea
P = thisShape.ReturnLength
if (thisShape.IsNull or (P*A = 0)) then 
  return {thisShape.GetClass.MakeNull, 0.0}
end
'
' Bracket the (negative of the) buffering distance.
'
e0 = (1-r)*A/P                      ' Lower bound 
e1 = ((1-r)*A/Number.GetPi).Sqrt    ' Upper bound
'
' Find the buffering distance.
'
lstArgs = {NIL, thisShape, r*A, thisShape}
e = av.Run("NR.ZBRENT", {scrF, e0, e1, P*Tol, lstArgs})
'MsgBox.Info("Buffer distance is" ++ e.AsString, strTitle)
'
' Create the buffer.
'
if (e <> NIL) then
  shpNew = thisShape.ReturnBuffered(e.Negate)
else
  shpNew = thisShape.GetClass.MakeNull
  e = 0.0
end

return {shpNew, e}
' end of script
 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Tracy Chan 
Date Sep 04, 2002 
Message Greatly appreciate for the help of Mr. Huber. The scripts work perfect. Thank you.

I can now obtain the maximum and average width of polygons. The question is I don't know what unit are they. The map units and distance units I set for the view is mile and feet respectively. But the results fit more to meters. Which one is correct? Or what unit the calculation is based on?

Also, I changed the last sentence of the average width code to "return L" and ran the map calculator again and I got another set of numbers. Are they the average length of the polygons? I hope they are.

If I need the maximum and minimum length of polygons, how can I get them? can I get the results by just modifying some of the skeleton codes or I need a whole new script for this calculation?

Thank you very much for helping me to solve all these questions.

-Tracy Chan
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Sep 04, 2002 
Message It sounds like your polygons are in decimal degrees and you have a projection set. In that case, the results likely are in meters.

The "L" in the average width script is indeed the average length.

In general, once you have calculated a width, you can compute a corresponding length by dividing the area by the width:

[shape].ReturnArea / [Width]

(For calculations like these it is easiest to have projected your data already; otherwise, you need to resort to a script that will project from decimal degrees on the fly.)

Of course, the maximum width (computed using the skeleton scripts) will correspond to a _minimum_ length. 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author William Huber 
Date Sep 03, 2002 
Message Here is a fast and simple way to obtain an "average width," in some well-defined sense.

Your polygons are composed of thin rectangles. For a rectangle of length L and width W, A := area = LW and P := perimeter = 2(L+W). Having computed A and P (which are fast in ArcView), you can therefore solve for L and W.

This approach will not always work--it fails for a circle, for instance--but it should give good results for your data and is fast enough to apply to 60,000 polygons.

Use the code below in the Field Calculator. Copy the code to a script editor, name it (I named mine "Polygon.Width"), and compile it. Create a numeric field for the polygon widths. Use the Field Calculator expression

av.Run("Polygon.Width", [shape])

to compute the widths.

If you are familiar with the quadratic formula you might wonder why I didn't just compute W directly as (P - Delta.Sqrt)/4. The reason is that this form is numerically less reliable, especially for long skinny shapes.

To summarize, at this point you have a good working definition of "maximum width" based on the skeleton idea and a good working definition of "average width."

"Minimum width" is tougher. Intuitively, we could define it geographically: if the polygon is the map of an island, the minimum width is the shortest distance across the island.

To make this well-defined, we should demand that routes across the island which merely "cut off a corner" don't count; otherwise, one could cut arbitrarily small corners and conclude all polygons have minimum width of zero, which is not terribly useful.

One definition of what it means to cross the island without cutting a corner is that after making such a crossing, if you were to return to the starting point by following the shoreline (in either direction), you would have to circle at least halfway back.

More precisely, start at any point Q on the boundary of the polygon. Define two other boundary points (not necessarily distinct) Q' and Q'' by traveling in either direction from Q along the boundary until the first point at which your direction of travel has changed by at least 180 degrees.

The total change in direction as you circumnavigate the island is 360 degrees, so necessarily you pass through the 180 degree point at least once along the way. This implies Q' and Q'' always exist.

(There are complications dealing with lack of well-defined direction at the vertices, but these can be dealt with rigorously.)

Now *remove* all the shoreline along which you had to travel in going from Q to Q' and Q to Q'' (but don't remove Q' or Q'', respectively). Call the remaining portion of the boundary the "shore opposite Q." (It may be just the single point Q' = Q'', but it is never empty. It will also include any rings of the polygon other than the ring on which Q lies, if the polygon is multiply connected.)

We could define the minimum width of the island, then, to be the length of the shortest line segment on the island that connects a point of the boundary to its opposite shore.

(Alternative definitions include not requiring the line segment to lie entirely within the island, or replacing "line segment" by "continuous path.")

The results will not always square with your intuition: for example, the minimum width of the United States in this sense is probably just a few meters! All you need is some protrusion, some isthmus, however short. For simple polygons like yours, such bad behavior is unlikely. However, this simple strange example points out that a "minimum" width may not be a very useful concept in general.

Evidently a straightforward computation of this minimum width would require a lot of effort. It is feasible: besides inspecting each vertex (to calculate its opposite shore), you would have to inspect each segment. Fortunately, all points along a segment share the same opposite shore. So if we take our definition to mean "smallest distance between points and their opposite shores," we just compute the distances between each segment and its opposite shore, between each vertex and its opposite shore, and take the smallest of all these distances. This works for simple polygons and can be extended to handle multiply connected polygons. Evidently the maximum total effort is proportional to N^2 where N is the number of vertices. 
 
A = SELF.ReturnArea
P = SELF.ReturnLength

Delta = P^2 - (16*A)
if (Delta > 0.0) then
  L = (P + Delta.Sqrt)/4
else
  L = P/4
end
W = A/L
return W
 
  --Bill Huber
Quantitative Decisions (http://www.quantdec.com )
More GIS Q&A at http://gis.stackexchange.com/q/3083/664 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Kirsten Lawrence 
Date Sep 05, 2007 
Message I need to calculate min, max and average widths of polygons representing road features in ArcMap. Is there a VB version of this script around? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Polygon width and length calculation 
Author Dan Patterson 
Date Sep 05, 2007 
Message http://arcscripts.esri.com/details.asp?dbid=14535
will give you the minimum area rectangle long and short axis fit to the convex hull of the shapes 
  Geomatics, Carleton University, Ottawa, Canada