J'ai une couche d'entités polygonale que je voudrais numéroter automatiquement mais en spirale (En escargot). De facon à avoir une continuité par voisinage. je suis un débutant en python, merci de m'aider. Merci
While the OP snoozes away, I have gone down another avenue... extent centroid to polygon centroid, angle and distance calculations and ranking... lacking sufficient coffee, I will pack in for now
If only it were a fishnet... it would be so easy
I see, you want to flip between max Y and max X and min Y and min X pairs. A Peano sort won't cut it. I suspect you will have to generate the spiral from the centre with a repeating angle and an increasing distance increment. The rate at which the distance from the centre changes (ie what function, if any, sin, cos etc) will determine the shape of the spiral.
I am sure it has a name, but the market for spiral generation outside of mazes and shellfish is somewhat limited
Although a reversed squished Archimede's spiral looks promising https://en.wikipedia.org/wiki/Spiral
rigid solutions exist already in rosetta code https://rosettacode.org/wiki/Spiral_matrix#Python the trick is to oblate the spiral and smooth the steps
The parcels are in red and the black line is the neighborhood numbering direction.
Thank you Dan,
what do you think if I send you an example of a shapefile.
I can't apply it to my polygons.
Many thanks
Yes, I'm trying to understand his approach to adapting it. But it will not be for tomorrow.
Thanks
Hi Ned Charles ,
If you want to number them as a "spiral", I assume that distance from the first (center) also influences.the resulting numbering as well as the start angle:
or for instance this way
Can you provide a screenshot or sample of your data to see what it looks like?
Ok... I found some field calculations.
If you have a featureclass/shapefile, this field calculator will do the calculations for you.
You must have projected coordinates!
If you copy that file into a text editor and save it with as .... azimuth_to.cal .... you can load it up into the field calculator. It is the same principle, but it does the azimuth to... but you can then sort it using
Sort .... since you are only sorting on a regular field.
Normally the Sort tool (in Map or PRO) is only available at the advanced license if you want to sort on the Shape field or to use multiple fields.
<SPAN class="comment token"># -*- coding: UTF-8 -*-</SPAN> <SPAN class="string token">"""----------------------------------------- Input shape field: returns angle between 0 and <360 based upon the first and last point azimuth_to(!Shape!,from_x, from_y, from_north=True) ie azimuth_to(!Shape!, 300050, 5000050, True) """</SPAN> <SPAN class="keyword token">import</SPAN> math <SPAN class="keyword token">def</SPAN> <SPAN class="token function">azimuth_to</SPAN><SPAN class="punctuation token">(</SPAN>shape<SPAN class="punctuation token">,</SPAN> from_x<SPAN class="punctuation token">,</SPAN> from_y<SPAN class="punctuation token">,</SPAN> from_north<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> x <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>centroid<SPAN class="punctuation token">.</SPAN>X y <SPAN class="operator token">=</SPAN> shape<SPAN class="punctuation token">.</SPAN>centroid<SPAN class="punctuation token">.</SPAN>Y radian <SPAN class="operator token">=</SPAN> math<SPAN class="punctuation token">.</SPAN>atan2<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>y <SPAN class="operator token">-</SPAN> from_y<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>x <SPAN class="operator token">-</SPAN> from_x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> angle <SPAN class="operator token">=</SPAN> math<SPAN class="punctuation token">.</SPAN>degrees<SPAN class="punctuation token">(</SPAN>radian<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> from_north<SPAN class="punctuation token">:</SPAN> angle <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">450</SPAN> <SPAN class="operator token">-</SPAN> angle<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="number token">360</SPAN> <SPAN class="keyword token">return</SPAN> angle __esri_field_calculator_splitter__ azimuth_to<SPAN class="punctuation token">(</SPAN>!Shape!<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">300050</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5000050</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
NOW see the last line... the 300050 and 5000050 are the centre X and Y values... you will need to use the python parser in the field calculator and determine and enter your central points. If you want them from North, then entre True, otherwise you get the angles from the X-axis.
If set to False, then you have to sort in descending order.
if set to True, then you will be sorting in ascending order.
Hope this is clear as mud... draw a picture if it helps.
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">angle_2pnts</SPAN><SPAN class="punctuation token">(</SPAN>p0<SPAN class="punctuation token">,</SPAN> p1<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Two point angle : angle = atan2(vector2.y, vector2.x) - atan2(vector1.y, vector1.x); : Accepted answer from the poly_angles link """</SPAN> ba <SPAN class="operator token">=</SPAN> p1 <SPAN class="operator token">-</SPAN> p0 ang_ab <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arctan2<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>ba<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> np<SPAN class="punctuation token">.</SPAN>rad2deg<SPAN class="punctuation token">(</SPAN>ang_ab <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">*</SPAN> np<SPAN class="punctuation token">.</SPAN>pi<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> p1 <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> p0 <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> angle_2pnts<SPAN class="punctuation token">(</SPAN>p0<SPAN class="punctuation token">,</SPAN> p1<SPAN class="punctuation token">)</SPAN> <SPAN class="number token">45.0</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Sadly for some, I use numpy to perform all my calculations, but this may help
p0... consider that your center point
p1.... pN are all your other points.
If you had a list of points, I could modify my example or you could translate this into straight python using elementary geometry
In short, the angles are relative to the X-axis ranging from 180 to -180... sort in descending order to give you a clockwise direction, sort in ascending order to get counterclockwise.
If you have data in a particular form, I can provide a field calculator expression, and I have a toolbox somewhere that does radial, lexicographic and other sorting types if needed (I have probably done a blog post at some time as well)
See Dan Patterson's comment in this thread: numbering polygon in clockwise. Although the message doesn't have a code example, it gives a suggestion on how this problem might be approached.
Thank you, I would like to have a spiral in a clockwise !
Many thanks!
Translation: I have a layer of polygonal entities that I would like to automatically number but spiral. In order to have continuity by neighborhood. I am a beginner in python, thank you for helping me. Thank you
Question: Do you want the spiral in a clockwise or counterclockwise direction? ( Voulez-vous la spirale dans le sens des aiguilles d'une montre ou dans le sens contraire?)
Tagging https://community.esri.com/community/developers/gis-developers/python
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.