Search code examples
pythonfor-loopgismapboxarcgis

Python: for loop stops at first row when reading from csv


With this code, I can get it to fire thru the first row in a csv and post the content. Without the for loop, it works great as well. I am also, using a simple print statement, able to print out all of the rows in the csv. Where I'm getting stuck is how to get this to loop thru my csv (2300 rows) and replace two inline variable. I've tried a couple of iterations of this, moving statements around, etc, this is my latest attempt.

from __future__ import print_function
import arcrest
import json
import csv
if __name__ == "__main__":
    username = "uid"
    password = "pwd"
    portalId = "id"
    url = "http://www.arcgis.com/"
    thumbnail_url = ""
with open('TILES.csv') as csvfile:
    inputFile = csv.DictReader(csvfile)
    x = 0 # counter to display file count
    for row in inputFile:
        if x == 0:
            map_json = {
  "operationalLayers": [
    {
      "templateUrl": "https://{subDomain}.tiles.mapbox.com/v4/abc.GRSM_"+row['ID']+"_pink/{level}/{col}/{row}.png?access_token=pk.secret",
      "id": "GRSM_SPECIES_OBSERVATIONS_MAXENT_5733",
      "type": "WebTiledLayer",
      "layerType": "WebTiledLayer",
      "title": row['Species']+" Prediction",
      "copyright": "GRSM",
      "fullExtent": {
        "xmin": -20037508.342787,
        "ymin": -20037508.34278,
        "xmax": 20037508.34278,
        "ymax": 20037508.342787,
        "spatialReference": {
          "wkid": 102100
        }
      },
      "subDomains": [
        "a",
        "b",
        "c",
        "d"
      ],
      "visibility": True,
      "opacity": 1
    }
  ],
  "baseMap": {
    "baseMapLayers": [
      {
        "id": "defaultBasemap",
        "layerType": "ArcGISTiledMapServiceLayer",
        "opacity": 1,
        "visibility": True,
        "url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
      }
    ],
    "title": "Topographic"
  },
  "spatialReference": {
    "wkid": 102100,
    "latestWkid": 3857
  },
  "version": "2.0"
}
    securityHandler = arcrest.AGOLTokenSecurityHandler(username,
                                                       password)
    #   Create the administration connection
    #
    admin = arcrest.manageorg.Administration(url, securityHandler)
    #   Access the content properties to add the item
    #
    content = admin.content
    #   Get the user    #
    user = content.users.user()
    #   Provide the item parameters
    #
    itemParams = arcrest.manageorg.ItemParameter()
    itemParams.title = "GRSM_"+row['Species']
    itemParams.thumbnailurl = ""
    itemParams.type = "Web Map"
    itemParams.snippet = "Maxent Output: "+row['Species']
    itemParams.licenseInfo = "License"
    itemParams.accessInformation = "Credits"
    itemParams.tags = "Maxent"+row['Species']
    itemParams.description = "This map depicts the tiled output of a Maxent model depicting the probability of occurrence of "+row['Species']+". An in-line legend is not available for this map. "
    itemParams.extent = "-84.1076,35.2814,-82.9795, 35.8366"

    #   Add the Web Map
    #
    print (user.addItem(itemParameters=itemParams,
                              overwrite=True,
                              text=json.dumps(row)))
    x = x + 1

Here's the csv:

Species,ID
Abacion_magnum,0000166
Abaeis_nicippe,0000169
Abagrotis_alternata,0000172
Abies_fraseri,0000214
Ablabesmyia_mallochi,0000223
Abrostola_ovalis,0000232
Acalypha_rhomboidea,0000253
Acanthostigma_filiforme,0000296
Acanthostigma_minutum,0000297
Acanthostigma_multiseptatum,0000298
Acentrella_ampla,0000314
Acer_negundo,0000330
Acer_pensylvanicum,0000333
Acer_rubrum_v_rubrum,0000337
Acer_rubrum_v_trilobum,0000338
Acer_saccharum,0000341
Acer_spicatum,0000343

Solution

  • I think your indentation is wrong, you only have inside your for loop the if and the json:

     if x == 0:
          map_json = {
      "operationalLayers": [
        {
          "templateUrl": "https://{subDomain}.tiles.mapbox.com/v4/abc.GRSM_"+row['ID']+"_pink/{level}/{col}/{row}.png?access_token=pk.secret",
          "id": "GRSM_SPECIES_OBSERVATIONS_MAXENT_5733",
          "type": "WebTiledLayer",
          "layerType": "WebTiledLayer",
          "title": row['Species']+" Prediction",
          "copyright": "GRSM",
          "fullExtent": {
            "xmin": -20037508.342787,
            "ymin": -20037508.34278,
            "xmax": 20037508.34278,
            "ymax": 20037508.342787,
            "spatialReference": {
              "wkid": 102100
            }
          },
          "subDomains": [
            "a",
            "b",
            "c",
            "d"
          ],
          "visibility": True,
          "opacity": 1
        }
      ],
      "baseMap": {
        "baseMapLayers": [
          {
            "id": "defaultBasemap",
            "layerType": "ArcGISTiledMapServiceLayer",
            "opacity": 1,
            "visibility": True,
            "url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
          }
        ],
        "title": "Topographic"
      },
      "spatialReference": {
        "wkid": 102100,
        "latestWkid": 3857
      },
      "version": "2.0"
    }