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.
@yovavzo ,
The following script should work:
%Expression:
re.sub(compiling, mround, !SHAPE.wkt!)
%Code Block:
import re
compiling = re.compile(r"\d*\.\d+")
def mround(wkt):
return "{:.3f}".format(float(wkt.group()))
def regex(!WKT!):
return re.replace (!wkt!
you switch text case... !WKT! keep it consistent if that is the name of the field fix the re.replace section
@yovavzo ,
The following script should work:
%Expression:
re.sub(compiling, mround, !SHAPE.wkt!)
%Code Block:
import re
compiling = re.compile(r"\d*\.\d+")
def mround(wkt):
return "{:.3f}".format(float(wkt.group()))
@yovavzo ,
You changed the code I provided, in your Code Block, that's why you're getting the error.
I also tried it on your dataset and I'm getting the right output.
Input: MULTIPOLYGON (((198102.99230000004 666117.40980000049, 198102.99230000004 656117.40980000049, 208102.99230000004 656117.40980000049, 208102.99230000004 666117.40980000049, 198102.99230000004 666117.40980000049)))
Output: MULTIPOLYGON (((198102.992 666117.410, 198102.992 656117.410, 208102.992 656117.410, 208102.992 666117.410, 198102.992 666117.410)))
My bad
It worked 🙂
Many thanks
perhaps the following.
You have to use the mround function and pass in the SHAPE field I think
%Expression:
mround(!SHAPE!)
%Code Block:
import re
def mround(!SHAPE!):
compiling = re.compile(r"\d*\.\d+")
re.sub(compiling, mround, !SHAPE!.wkt)
return "{:.3f}".format(float(wkt.group()))
Thanks
Got error:
File "<string>", line 2
def mround (!SHAPE!):
SyntaxError: Invalid Sytax
%Code Block:
import re
def mround(fld):
compiling = re.compile(r"\d*\.\d+")
re.sub(compiling, mround, fld.wkt!)
return "{:.3f}".format(float(wkt.group()))
For a moment @DanPatterson , I thought you were proposing a regex solution right off the bat, and then I realized you were tweaking the OP's regex. 🙂