<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best way to coerce multi-geometry to single for WKT use in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-way-to-coerce-multi-geometry-to-single-for/m-p/1180724#M64727</link>
    <description>&lt;P&gt;Not sure if "what's the best way" questions are frowned on, but before I burn a bunch of cycles fixing this, I figured there might be a capability buried in ArcPy that I'm missing.&lt;/P&gt;&lt;P&gt;I'm attempting to get WKT of what appears to be a simple line feature in ArcGIS Pro.&lt;/P&gt;&lt;P&gt;When I use the &lt;A href="mailto:SHAPE@WKT" target="_blank"&gt;SHAPE@WKT&lt;/A&gt;&amp;nbsp;token to call it, it returns as&lt;/P&gt;&lt;P&gt;MULTILINESTRING Z ((10.0 20.0 0, 11.0 21.0 0,...))&lt;/P&gt;&lt;P&gt;I would like to return the feature as&lt;/P&gt;&lt;P&gt;LINESTRING((10.0 20.0,11.0 21.0,12.0 22.0,...))&lt;/P&gt;&lt;P&gt;as I am issuing these geometries as query criteria to various web services that are looking for simple OGC WKT.&lt;/P&gt;&lt;P&gt;Is there preprocessing available to me to ensure the &lt;A href="mailto:SHAPE@WKT" target="_blank"&gt;SHAPE@WKT&lt;/A&gt;&amp;nbsp;returns as a LINESTRING or POLYGON without Z, vs. MULTILINESTRING or MULTIPOLYGON?&amp;nbsp; I can brute force this with re patterns and a dictionary of replacements, but I was hoping for something slicker and more idiomatically "esri."&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jun 2022 17:43:07 GMT</pubDate>
    <dc:creator>EricEagle</dc:creator>
    <dc:date>2022-06-07T17:43:07Z</dc:date>
    <item>
      <title>Best way to coerce multi-geometry to single for WKT use</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-coerce-multi-geometry-to-single-for/m-p/1180724#M64727</link>
      <description>&lt;P&gt;Not sure if "what's the best way" questions are frowned on, but before I burn a bunch of cycles fixing this, I figured there might be a capability buried in ArcPy that I'm missing.&lt;/P&gt;&lt;P&gt;I'm attempting to get WKT of what appears to be a simple line feature in ArcGIS Pro.&lt;/P&gt;&lt;P&gt;When I use the &lt;A href="mailto:SHAPE@WKT" target="_blank"&gt;SHAPE@WKT&lt;/A&gt;&amp;nbsp;token to call it, it returns as&lt;/P&gt;&lt;P&gt;MULTILINESTRING Z ((10.0 20.0 0, 11.0 21.0 0,...))&lt;/P&gt;&lt;P&gt;I would like to return the feature as&lt;/P&gt;&lt;P&gt;LINESTRING((10.0 20.0,11.0 21.0,12.0 22.0,...))&lt;/P&gt;&lt;P&gt;as I am issuing these geometries as query criteria to various web services that are looking for simple OGC WKT.&lt;/P&gt;&lt;P&gt;Is there preprocessing available to me to ensure the &lt;A href="mailto:SHAPE@WKT" target="_blank"&gt;SHAPE@WKT&lt;/A&gt;&amp;nbsp;returns as a LINESTRING or POLYGON without Z, vs. MULTILINESTRING or MULTIPOLYGON?&amp;nbsp; I can brute force this with re patterns and a dictionary of replacements, but I was hoping for something slicker and more idiomatically "esri."&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 17:43:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-coerce-multi-geometry-to-single-for/m-p/1180724#M64727</guid>
      <dc:creator>EricEagle</dc:creator>
      <dc:date>2022-06-07T17:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to coerce multi-geometry to single for WKT use</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-coerce-multi-geometry-to-single-for/m-p/1180752#M64728</link>
      <description>&lt;P&gt;Here's the super duct tapey way I handle this now, by the way:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import re


def get_wkts(self, lyr):
    # This is part of a larger python toolbox class
    wkts = []
    with_z = r"(\d+\.\d+) (\d+.\d+) (\d+,?)"
    without_z = r"(\d+.\d+) (\d+.\d+,?)"
    typemap = {
        "MULTILINESTRING (": ["LINESTRING", without_z, "((", "))"],
        "MULTILINESTRING Z (": ["LINESTRING", with_z, "((", "))"],
        "MULTIPOLYGON (": ["POLYGON", without_z, "(((", ")))"],
        "MULTIPOLYGON Z (": ["POLYGON", with_z, "(((", ")))"],
        "POINT (": ["POINT", without_z, "(", ")"],
        "POINT Z (": ["POINT", with_z, "(", ")"]
    }

    with arcpy.da.SearchCursor(lyr, ["SHAPE@WKT"]) as cur:
        for row in cur:
            for k, v in typemap.items():
                if row[0].startswith(k):
                    wkt_prefix = v[0]
                    coords = v[2] + ",".join([str(f[0] + " " + f[1]) for f in re.findall(v[1], row[0])]) + v[3]
                    wkts.append(wkt_prefix + coords)
    return wkts&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 18:49:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-coerce-multi-geometry-to-single-for/m-p/1180752#M64728</guid>
      <dc:creator>EricEagle</dc:creator>
      <dc:date>2022-06-07T18:49:37Z</dc:date>
    </item>
  </channel>
</rss>

