<?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 Re: Add a feature layer (published from a csv file) to a webmap in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316561#M8918</link>
    <description>&lt;P&gt;BTW, this is using version 2.1.0.3 of the arcgis python package.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Aug 2023 18:17:43 GMT</pubDate>
    <dc:creator>DonaldPike</dc:creator>
    <dc:date>2023-08-08T18:17:43Z</dc:date>
    <item>
      <title>Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316552#M8917</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;I have an existing feature layer published from a csv file that I'm trying to add to a webmap. I have the following code that runs without errors but when I go back into the GUI (through ArcGIS online) the layer doesn't show up as having been added.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.mapping import WebMap
from arcgis.features import FeatureLayer
from pprint import pprint

print("Logging in...")
gis = GIS("https://fotnf.maps.arcgis.com/", "friendsofthetonto", "XXXXXXXX")
print(f"Connected to {gis.properties.portalHostname} as {gis.users.me.username}")

webmap_item = gis.content.get('e1405425e52d43689cfdaecd43e0239d')
webmap = WebMap(webmap_item)
csv_item2 = gis.content.get('ec6e29fe17a44512887d761f5e3524d6')

csv_layer2 = FeatureLayer(csv_item2.url)

webmap.add_layer(csv_item2)
webmap_item.update(data=webmap)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 18:11:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316552#M8917</guid>
      <dc:creator>DonaldPike</dc:creator>
      <dc:date>2023-08-08T18:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316561#M8918</link>
      <description>&lt;P&gt;BTW, this is using version 2.1.0.3 of the arcgis python package.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 18:17:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316561#M8918</guid>
      <dc:creator>DonaldPike</dc:creator>
      <dc:date>2023-08-08T18:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316573#M8919</link>
      <description>&lt;P&gt;Creating a FeatureLayer requires the URL to be the full path to the individual layer. I might have a URL like this:&lt;/P&gt;&lt;P&gt;&lt;A href="https://maps.co.kendall.il.us/server/rest/services/Hosted/Cemeteries/FeatureServer" target="_blank"&gt;https://maps.co.kendall.il.us/server/rest/services/Hosted/Cemeteries/FeatureServer&lt;/A&gt;&lt;/P&gt;&lt;P&gt;But that's the URL for the &lt;STRONG&gt;service&lt;/STRONG&gt;. The layer itself is:&lt;/P&gt;&lt;P&gt;&lt;A href="https://maps.co.kendall.il.us/server/rest/services/Hosted/Cemeteries/FeatureServer/0" target="_blank"&gt;https://maps.co.kendall.il.us/server/rest/services/Hosted/Cemeteries/FeatureServer/0&lt;/A&gt;&lt;/P&gt;&lt;P&gt;If your service only has a single layer in it, you can access it by using &lt;STRONG&gt;csv_item2.layers[0]&lt;/STRONG&gt;, which returns a &lt;STRONG&gt;FeatureLayer&lt;/STRONG&gt; object, and you can bypass calling the FeatureLayer constructor.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 18:37:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316573#M8919</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2023-08-08T18:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316693#M8920</link>
      <description>&lt;P&gt;Thanks for the suggestion.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried changing the code to the following (passing csv_item2.layers[0] directly into webmap.add_layer()):&lt;/P&gt;&lt;LI-CODE lang="c"&gt;from arcgis.gis import GIS
from arcgis.mapping import WebMap
from arcgis.features import FeatureLayer
from pprint import pprint

print("Logging in...")
gis = GIS("https://fotnf.maps.arcgis.com/", "friendsofthetonto", "XXXXXXXX")
print(f"Connected to {gis.properties.portalHostname} as {gis.users.me.username}")

webmap_item = gis.content.get('e1405425e52d43689cfdaecd43e0239d')
webmap = WebMap(webmap_item)
csv_item2 = gis.content.get('ec6e29fe17a44512887d761f5e3524d6')

# csv_layer2 = FeatureLayer(csv_item2.url)

webmap.add_layer(csv_item2.layers[0])
webmap_item.update(data=webmap)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I'm still getting the same result where the code runs without errors but the new layer doesn't show up.&lt;/P&gt;&lt;P&gt;Here's a screencast of the issue:&lt;BR /&gt;&lt;A href="https://d.pr/v/RNdZiv" target="_blank"&gt;https://d.pr/v/RNdZiv&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 23:33:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1316693#M8920</guid>
      <dc:creator>DonaldPike</dc:creator>
      <dc:date>2023-08-08T23:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1321539#M8949</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/332920"&gt;@DonaldPike&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your csv_item2 is a CSV item you will need to publish the CSV Item as a Feature Service first before you can add the Feature Layer from within this Feature Service to a WebMap. (it can aslo be published as a Table Service and added to a WebMap as a Table)&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2023 10:58:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1321539#M8949</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2023-08-23T10:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: Add a feature layer (published from a csv file) to a webmap</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1326620#M8986</link>
      <description>&lt;P&gt;I was eventually able to get this working. Here's the code that we ended up with to publish and add the csv layer to an existing webmap:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def update(_args):
    data_layer_definition = (
        get_polys_layout_default()
        if _args.is_polys
        else get_points_layout_default()
    )
    popup_config = (
        get_polys_popup_config_default()
        if _args.is_polys
        else get_points_popup_config_default()
    )
    gis = _args.gis
    # UPLOAD CSV
    snake_case_random = uuid.uuid1()
    points_or_polys_text = "polys" if _args.is_polys else "points"
    csv_file_orig = _args.data_path / f"Joined.csv"
    csv_file_new = _args.data_path / f"Joined_{snake_case_random}.csv"
    shutil.copy(csv_file_orig.resolve(), csv_file_new.resolve())
    csv_agol = gis.content.add(
        item_properties={
            "title": f"Temp CSV {points_or_polys_text} {snake_case_random}",
        },
        data=csv_file_new.as_posix(),
    )
    csv_agol.move(f"Oliver")
    feature_layer = csv_agol.publish()
    # ASSOCIATE CSV WITH WEBMAP
    webmap_item = gis.content.get("a752f298d15c4767a34e0b4981245afc")
    web_map_definition = webmap_item.get_data()
    title = "Points Status Layer"
    if _args.is_polys:
        title = "Polys Status Layer"
    layer_index = None
    arcpy.AddMessage("Here 40")
    arcpy.AddMessage(len(web_map_definition["operationalLayers"]))
    for index, layer in enumerate(web_map_definition["operationalLayers"]):
        if layer["title"].startswith(title):
            layer_index = index
            break
    # arcpy.AddMessage("Here 50")
    # arcpy.AddMessage(web_map_definition["operationalLayers"][layer_index])
    arcpy.AddMessage(f"Layer index: {layer_index}")
    arcpy.AddMessage("Here 50")
    # arcpy.AddMessage(web_map_definition["operationalLayers"][layer_index]["popupInfo"])
    # exit()
    if layer_index is None:
        # Add the layer since it doesn't already exist.
        arcpy.AddMessage("Adding layer")
        # Add the feature layer's URL to the web map definition's
        # operationalLayers
        web_map_definition["operationalLayers"].append(
            {
                "id": feature_layer.id,
                "layerType": "ArcGISFeatureLayer",
                "url": feature_layer.url,
                "visibility": True,
                "layerDefinition": data_layer_definition,
                "opacity": 1,
                "title": f"{title} {_args.current_day.strftime('%Y-%m-%d')}",
                "popupInfo": popup_config,
            }
        )
    else:
        # Replace the existing layer
        arcpy.AddMessage("Replacing layer")

        web_map_definition["operationalLayers"][layer_index][
            "id"
        ] = feature_layer.id
        web_map_definition["operationalLayers"][layer_index][
            "url"
        ] = feature_layer.url

        # Update the title with the current date
        web_map_definition["operationalLayers"][layer_index][
            "title"
        ] = f"{title} {_args.current_day.strftime('%Y-%m-%d')}"

    arcpy.AddMessage(web_map_definition["operationalLayers"])
    # Add the feature layer's URL to the web map definition's operationalLayers
    # Update the web map's definition
    webmap_item.update(data=web_map_definition)
    arcpy.AddMessage(f"Done")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 22:16:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-feature-layer-published-from-a-csv-file-to-a/m-p/1326620#M8986</guid>
      <dc:creator>DonaldPike</dc:creator>
      <dc:date>2023-09-07T22:16:29Z</dc:date>
    </item>
  </channel>
</rss>

