Format WKT to nearest meter

2734
4
Jump to solution
01-18-2016 07:29 AM
FilipKrál
Occasional Contributor III

Hi,

I have a polygon feature class and I want to produce a text file where each feature is a row with an ID and the polygon geometry formatted as Well Known Text (WKT).

To start with I am looping though the features and I use the .WKT property to produce the result.

My problem is that the WKT is often formatted with coordinates reported to 12 decimal places. I need to round the coordinates to the nearest meter. Ideally I want to be able to do this with other types of geometries so I'd prefer something else then looping over the rings and vertices.

Is there a way to do this? I tried to set arcpy.env.XYTolerance and arcpy.env.XYResolution to 1.0 but that had no effect.

Any hints?

Filip.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

There is a way... if you make a copy of the data using the settings you alread tried.

import arcpy

fc = r"D:\Xander\EPM\Datos pruebas\gdb\datos erase.gdb\line_copy_proj"

fc_tmp = r"IN_MEMORY\test"

arcpy.env.XYResolution = 1

arcpy.env.XYTolerance = 1

arcpy.CopyFeatures_management(fc, fc_tmp)

flds = ('SHAPE@WKT')

with arcpy.da.SearchCursor(fc_tmp, flds) as cursor:

    for row in cursor:

        print row[0]

original output:

MULTILINESTRING ((836240.86539999954 1179632.7086999994, 837135.58619999979 1181670.2111000009, 839675.22300000023 1183242.5173000004, 839712.04700000025 1184657.2405999992, 839258.58920000028 1187356.5383000001, 839133.95399999991 1189890.4180999994))

MULTILINESTRING ((833475.98560000025 1180002.4503000006, 834079.72329999972 1183883.3637000006, 834317.80570000038 1186547.8800000008, 835274.46169999987 1187203.2232000008, 837515.99909999967 1188183.9852000009))

MULTILINESTRING ((837624.12339999992 1191506.9096000008, 834850.68890000042 1188685.0925999992, 833435.97180000041 1189018.1926000006, 833605.04540000018 1190564.1831999999))

after making the copy in memory:

MULTILINESTRING ((836241 1179633, 837136 1181670, 839675 1183243, 839712 1184657, 839259 1187357, 839134 1189890))

MULTILINESTRING ((833476 1180002, 834080 1183883, 834318 1186548, 835274 1187203, 837516 1188184))

MULTILINESTRING ((837624 1191507, 834851 1188685, 833436 1189018, 833605 1190564))

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Are they projected coordinates or geographic? since chopping off decimal points in geographic coordinates isn't going to get you to your desired meter cutoff since the decimal places are a function of position

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

I would take a look at 45376 - Export coordinates of polygon vertices to a TXT file   just add two new fields with the field precision set to what you want, and do a simple calculation? Do this before you export your file using those fields.  Sometimes simple is best (unless you are doing it multiple times, in which case a python script would help),

if if you are needing the cordinates in a different corrdinate system than they are currently, there are several ways to get those coordinates one you have your polygon converted to points, including ArcGIS Help 10.2 - Convert Coordinate Notation (Data Management) 

or for 10.3   Convert Coordinate Notation—Help | ArcGIS for Desktop

0 Kudos
XanderBakker
Esri Esteemed Contributor

There is a way... if you make a copy of the data using the settings you alread tried.

import arcpy

fc = r"D:\Xander\EPM\Datos pruebas\gdb\datos erase.gdb\line_copy_proj"

fc_tmp = r"IN_MEMORY\test"

arcpy.env.XYResolution = 1

arcpy.env.XYTolerance = 1

arcpy.CopyFeatures_management(fc, fc_tmp)

flds = ('SHAPE@WKT')

with arcpy.da.SearchCursor(fc_tmp, flds) as cursor:

    for row in cursor:

        print row[0]

original output:

MULTILINESTRING ((836240.86539999954 1179632.7086999994, 837135.58619999979 1181670.2111000009, 839675.22300000023 1183242.5173000004, 839712.04700000025 1184657.2405999992, 839258.58920000028 1187356.5383000001, 839133.95399999991 1189890.4180999994))

MULTILINESTRING ((833475.98560000025 1180002.4503000006, 834079.72329999972 1183883.3637000006, 834317.80570000038 1186547.8800000008, 835274.46169999987 1187203.2232000008, 837515.99909999967 1188183.9852000009))

MULTILINESTRING ((837624.12339999992 1191506.9096000008, 834850.68890000042 1188685.0925999992, 833435.97180000041 1189018.1926000006, 833605.04540000018 1190564.1831999999))

after making the copy in memory:

MULTILINESTRING ((836241 1179633, 837136 1181670, 839675 1183243, 839712 1184657, 839259 1187357, 839134 1189890))

MULTILINESTRING ((833476 1180002, 834080 1183883, 834318 1186548, 835274 1187203, 837516 1188184))

MULTILINESTRING ((837624 1191507, 834851 1188685, 833436 1189018, 833605 1190564))

View solution in original post

FilipKrál
Occasional Contributor III

Haha, Thank you very much, mister Xander Bakker​, that is the kind of "slide-of-hand-arcgis-trick" I was looking for. Nicely done, I'll go with that.

By the way, dropping the 11 digits after the decimal place of each coordinate reduced the size of the output text file from about 150 MB to about 50 MB!

To clarify the coordinate system, this was in British National Grid (EPSG:27700), i.e. projected in metres.

Thanks everyone,

Filip.

0 Kudos