Hi,
I need to reduce WKT (Well Known Text) length.
The reduction is done by rounding/trunkating decimal places from 11 to 3 - default output for the script !shape.wkt! returns coordinate list with 11 decimal places (in a metric projected coordinate system).
I ran across a RegEx expression that might do the job, but it returns an error (arcGIS Pro 2.7 - python):
%Expression:
regex(!WKT!)
%Code Block:
import re
def regex(!WKT!):
return re.replace (!wkt!, '(\d+. \d{4}) \d+)', '\1')
I'm not experianced python user.
I'd be happy to get help with solution - it doesnt has to be this expression - any creativ/simple solution (in Field Calculator) is welcomed.
I attached a sample layer (FGDB).
Thanks in advance,
Solved! Go to Solution.
tweaking, not my goto for sure 😉
Thanks again 🙂
Got another error (when checkd validation):
File "<string>", line 4
re.sub(compiling, mround, fld.wkt!)
SyntaxError: Invalid Sytax
you have a single ! if that is the problem. !fld.wkt! which look strange in the first place thought it would be !SHAPE@WKT! as is used in cursors
So my version didn't work? referencing the shape field?
Did you check my new reply?
You'r right
replied to your original reply - thanks
To follow-up or add on to @MehdiPira1 , you can make the precision a parameter of the function:
# expression
wkt_precision(!SHAPE.wkt!, 3)
# code block
import re
def wkt_precision(wkt, precision)
return re.sub("(\d+\.\d+)", lambda m: f"{float(m.group()):.{precision}f}", wkt)
Just a minor follow-up on this; depending on the complexity of your feature (especially dense, un-simplified multipolygons), reducing the precision of the WKT could cause some vertices to become coincident, break topology, or otherwise create invalid geometries that will make your table un-selectable within the ArcGIS Pro GUI.
I was dealing with some coastal polygons and this issue tripped me up and I ended up needing to move up to a precision 7 to negate the issue. Also, relevant XKCD: