Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a polygon shapefile with over 16k features. Each has an attribute (Double, Precision(10) Scale (2)) that I am trying to calculate the square root of, and store in new field. Is there a tool, field calculator code block, or script I can leverage?

Example: Square root of 16 is (4). (not looking for root mean square).

I see there are functions in Spatial Analyst for square root calculations on rasters, but I cant seem to locate any for shapefiles.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Using Field Calculator, either VB Script Sqr([FIELDNAME]) or Python math.sqrt(!FIELDNAME!) under the Number Type will yield square roots.

You can either calculate into a new field, or on the field itself.

share|improve this answer
    
Sorry for the delay, thanks for this solution. The issue i run into with this is the calculation runs into an error once the first record is reached that has a negative (-) value and then stops calculating the remaining records in the table/feature class. Any thoughts on how to address this? The reason for negative values is that the Square root calculation is part of a larger standard deviation outcome I am trying to accomplish. The square root calc is being applied to the variance. –  BJ Marraccini Sep 3 at 19:24
1  
Isn't the square of a negative number, the normal square root with 'i' added to the end? If so, change the numbers to positive, making sure to flag whether they were previously negative, and run the calculation –  Maksim Sep 3 at 19:31

Maksim already provided the answer with ArcGIS, so here is the (very similar) QGIS solution

right click on your shapefile to open its attribute table

click "toggle edit mode" button (a pencil) (or ctrl+E)

on the right, click the icon for field calculator (an abacus) (or ctrl + I)

write sqrt(name_of_field) (EDIT : or sqrt(abs(name_of_field)) when you need an output even with negative value)

Note that you can check "create in a new field" directly from the field calculator (contrary to ArcGIS where you need to create your field first, and run the field calculator on the field where you right-click) if you want.

share|improve this answer
    
This works just fine, except for square root of negative values. QGIS enters 'nan' (I'm assuming means 'Not A Number') for those records with a negative (-) value. Any thoughts on calculating square root on both positive and negative numbers? The reason for negative values is that the Square root calculation is part of a larger standard deviation outcome I am trying to accomplish. The square root calc is being applied to the variance. –  BJ Marraccini Sep 3 at 19:26
    
I gave Maksim the Accepted Answer since it came in first overall and works as a calculation. Your answer using QGIS also is correct, and works. Using Maksim's comment in the Esri answer below: "change the numbers to positive, making sure to flag whether they were previously negative, and run the calculation" - this approach will also work in QGIS. Thank You. –  BJ Marraccini Sep 3 at 19:37
    
I don't know any example of negative variance if you work with real number. Maybe it is a correlation value and not a variance. –  radouxju Sep 3 at 19:40
    
In this case the variance is calculated from 30 years of precipitation data, and for a given polygon the average difference from mean is negative (more years under mean than over, but the outliers have bumped the mean higher). –  BJ Marraccini Sep 3 at 19:43
    
maybe we can discuss this in the chat room, but variance is always positive. so you take the square root of the average difference, not the square root of the variance. –  radouxju Sep 3 at 19:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.