#and then run an arcpy code
Explore tagged Tumblr posts
scoobydoodean · 2 years ago
Text
But also IF I am around all day long and you can tell I won't get tf off Tumblr you should be looking at me like this to shame me as if I were Dean Winchester eating a whole pint of ice cream and you were Sam telling me I am going to get killed by a werewolf because I can no longer run fast enough to escape.
Tumblr media
I need to do soooo much actual work so if I am not around as much in the coming weeks that is why.
8 notes · View notes
many-leaves-on-the-trees · 5 years ago
Text
I was like, oh I’ll write a bit of Python code that integrates through some image files and also iterates through some polygons for clipping purposes. There’s already a purpose-built set of commands to do this stuff! It’ll be easy and I won’t have to do it manually!
Three days later (5 if you count the times I spent on the weekend writing the skeleton of it but couldn’t test it): alright we’re iterating through the polygons and getting their centroids, we’re matching locations with the image files, we’re selecting one polygon at a time with a SQL query, but why is the clip feature not working???
4 notes · View notes
timobook · 7 years ago
Text
Programming ArcGIS 10.1 with Python Cookbook
Programming ArcGIS 10.1 with Python Cookbook: Over 75 recipes to help you automate geoprocessing tasks, create solutions, and solve problems for ArcGIS with Python Download Introduction ArcGIS is an industry-standard geographic information system from ESRI. This book will show you how to use the Python programming language to create geoprocessing scripts, tools, and shortcuts for the ArcGIS Desktop environment. This book will make you a more effective and efficient GIS professional, by showing you how to use the Python programming language with ArcGIS Desktop to automate geoprocessing tasks, manage map documents and layers, find and fix broken data links, edit data in feature classes and tables, and much more. Programming ArcGIS 10.1 with Python Cookbook starts by covering fundamental Python programming concepts in an ArcGIS Desktop context. Using a how-to instruction style, you'll then learn how to use Python to automate common important ArcGIS geoprocessing tasks. In this book, you will also cover specific ArcGIS scripting topics that will help save you time and effort when working with ArcGIS. Topics include managing map document files, automating map production and printing, finding and fixing broken data sources, creating custom geoprocessing tools, and working with feature classes and tables, among others. In Programming ArcGIS 10.1 with Python Cookbook, you'll learn how to write geoprocessing scripts using a pragmatic approach designed around accomplishing specific tasks in a cookbook style format. What this book covers Chapter 1, Fundamentals of the Python Language for ArcGIS, will cover many of the basic language constructs found in Python. Initially, you'll learn how to create new Python scripts or edit existing scripts. From there, you'll get into language features, such as adding comments to your code, variables, and the built-in typing systems that makes coding with Python easy and compact. Furthermore, we'll look at the various built-in data-types that Python offers, such as strings, numbers, lists, and dictionaries. In addition to this, we'll cover statements, including decision support and looping structures for making decisions in your code and/or looping through a code block multiple times. Chapter 2, Writing Basic Geoprocessing Scripts with ArcPy, will teach the basic concepts of the ArcPy Python site package for ArcGIS, including an overview of the basic modules, functions, and classes. The reader will be able write a geoprocessing script using ArcPy with Python. Chapter 3, Managing Map Documents and Layers, will use the Arcpy Mapping module to manage map document and layer files. You will learn how to add and remove geographic layers from map document files, insert layers into data frames, and move layers around within the map document. The reader will also learn how to update layer properties and symbology. Chapter 4, Finding and Fixing Broken Data Links, will teach how to generate a list of broken data sources in a map document file and apply various Arcpy Mapping functions to fix these data sources. The reader will learn how to automate the process of fixing data sources across many map documents. Chapter 5, Automating Map Production and Printing, will teach how to automate the process of creating production-quality maps. These maps can then be printed, exported to image file formats, or exported to PDF files for inclusion in map books. Chapter 6, Executing Geoprocessing Tools from Scripts, will teach how to write scripts that access and run geoprocessing tools provided by ArcGIS. Chapter 7, Creating Custom Geoprocessing Tools, will teach how to create custom geoprocessing tools that can be added to ArcGIS and shared with other users. Custom geoprocessing tools are attached to a Python script that process or analyze geographic data in some way. Chapter 8, Querying and Selecting Data, will teach how to execute the Select by Attribute and Select by Location geoprocessing tools from a script to select features and records. The reader will learn how to construct queries that supply an optional where clause for the Select by Attribute tool. The use of feature layers and table views as temporary datasets will also be covered. Chapter 9, Using the ArcPy Data Access Module to Select, Insert, and Update Geographic Data and Tables, will teach how to create geoprocessing scripts that select, insert, or update data from geographic data layers and tables. With the new ArcGIS 10.1 Data Access module, geoprocessing scripts can create in-memory tables of data, called cursors, from feature classes and tables. The reader will learn how to create various types of cursors including search, insert, and update Chapter 10, Listing and Describing GIS Data, will teach how to obtain descriptive information about geographic datasets through the use of the Arcpy Describe function. As the first step in a multi-step process, geoprocessing scripts frequently require that a list of geographic data be generated followed by various geoprocessing operations that can be run against these datasets. Chapter 11, Customizing the ArcGIS Interface with Add-Ins, will teach how to customize the ArcGIS interface through the creation of Python add-ins. Add-ins provide a way of adding user interface items to ArcGIS Desktop through a modular code base designed to perform specific actions. Interface components can include buttons, tools, toolbars, menus, combo boxes, tool palettes, and application extensions. Add-ins are created using Python scripts and an XML file that define how the user interface should appear. Chapter 12, Error Handling and Troubleshooting, will teach how to gracefully handle errors and exceptions as they occur while a geoprocessing script is executing. Arcpy and Python errors can be trapped with the Python try/except structure and handled accordingly. Appendix A, Automating Python Scripts, will teach how to schedule geoprocessing scripts to run at a prescribed time. Many geoprocessing scripts take a long time to fully execute and need to be scheduled to run during non-working hours on a regular basis. The reader will learn how to create batch file containing geoprocessing scripts and execute these at a prescribed time. Appendix B, Five Things Every GIS Programmer Should Know How to Do with Python, will teach how to write scripts that perform various general purpose tasks with Python. Tasks, such as reading and writing delimited text files, sending e-mails, interacting with FTP servers, creating ZIP files, and reading and writing JSON and XML files are common. Every GIS programmer should know how to write Python scripts that incorporate this functionality What you need for this book To complete the exercises in this book, you will need to have installed ArcGIS Desktop 10.1 at either the Basic, Standard, or Advanced license level. Installing ArcGIS Desktop 10.1 will also install Python 2.7 along with the IDLE Python code editor. Who this book is for Programming ArcGIS 10.1 with Python Cookbook is written for GIS professionals who wish to revolutionize their ArcGIS workflow with Python. Whether you are new to ArcGIS or a seasoned professional, you almost certainly spend time each day performing various geoprocessing tasks. This book will teach you how to use the Python programming language to automate these geoprocessing tasks and make you a more efficient and effective GIS professional. Via TimoBook
0 notes
uropsalis · 8 years ago
Text
día cuatro
Python 3
open keyword
text = “I put information here”
file = open(r"c:\users\johnsmith7\documents\file.txt", 'w') file.write(text) file.close()
Open keyword allows the creation of a file if it doesn’t exist and can add text to that file.  First line creates the string to add.  The open keyword then opens the file you want to open or create.  I put r before my string to avoid a unicodeecape because of the common file-path involving “Users” and also important to write the actual file and not just a directory location. At the very end of my open line, I put ‘w’ which tells python to write, but you can also read or append file if you wish (r, r+, w+, a and others).  Then finish the process with the .write(text), which actually writes it.  Also .close() to finish the process.  After that you can go to the directory to see your new file.
ArcPy
With tool execution, messages are written that tell you information such as when an operation started/ended, parameter values, warnings, and errors.  These messages appear in the result menu in arcgis.  With python, you can print the messages out or write them to file.  Error messages have ID codes can be looked up in the help file.  The GetMessages() function returns all the messages from a tool that was last used.  It can also filter the types of messages.  
Result() also returns information about executed tools with messages, parameters, and outputs.  But these results can be returned even have several other tools have been run.  Need to return here and detail properties/methods.
0 notes
mrrolandtfranco · 8 years ago
Text
Updating your hosted feature services with ArcGIS Pro and the ArcGIS API for Python
Many organizations share public maps on ArcGIS online and have defined a process to update and synchronize the feature layers from their local data. One method is well defined using ArcMap; you select the option to overwrite the hosted feature layers when publishing your updates back to ArcGIS online. In 2013 and 2014 we explained how you could update your hosted feature services automatically, at a prescribed time, using Python. In the spirit of this incredibly popular workflow to schedule updates, this blog will provide simpler Python code to update services using ArcGIS Pro 1.4 and the new ArcGIS API for Python.
This post assumes you’ve already shared a web map from ArcGIS Pro to ArcGIS online. You’ll need to make note of the user name and password of the publisher account, the service name as well as the original project file (.aprx). When publishing a web map from ArcGIS Pro, the underlying feature layers may have _WFL1 appended to the name to ensure they are unique within organization. Investigate your hosted content and be certain of the service definition (.sd) and feature layer names before proceeding.
The first step is to install the ArcGIS API for Python; from within ArcGIS Pro, find and add the arcgis package using the Python Package Manager. Alternatively, open a Python command prompt from: Start > ArcGIS > ArcGIS Pro > Python Command Prompt and use the following command, agreeing to update packages (if necessary):
 conda install -c esri arcgis
After installing the ArcGIS Python API you need to define the steps of your update process. Typically, this will involve one or more authors working on the local datasets that were shared to ArcGIS online. The source documents and data will be used to create a service definition (.sd) file using Python. At a prescribed time, perhaps every night at midnight, the local .sd file with your new data will be uploaded to ArcGIS online where it will be used to replace the hosted service.
Copy the following code and save it locally as a Python file (updatewebmap.py). This code creates the .sd file and upload/publishes it on ArcGIS online. A number of variables will need to be set within the script; the path to the project, service name username, password, metadata and sharing options when you create the file.
import arcpy import os, sys from arcgis.gis import GIS ### Start setting variables # Set the path to the project prjPath = r"C:\PROJECTS\NightlyUpdates\NightlyUpdates.aprx" # Update the following variables to match: # Feature service/SD name in arcgis.com, user/password of the owner account sd_fs_name = "MyPublicMap" portal = "http://ift.tt/1lM1Bk3; # Can also reference a local portal user = "UserName" password = "p@sswOrd" # Set sharing options shrOrg = True shrEveryone = False shrGroups = "" ### End setting variables # Local paths to create temporary content relPath = sys.path[0] sddraft = os.path.join(relPath, "WebUpdate.sddraft") sd = os.path.join(relPath, "WebUpdate.sd") # Create a new SDDraft and stage to SD print("Creating SD file") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps()[0] arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True) arcpy.StageService_server(sddraft, sd) print("Connecting to {}".format(portal)) gis = GIS(portal, user, password) # Find the SD, update it, publish /w overwrite and set sharing and metadata print("Search for original SD on portal...") sdItem = gis.content.search("{} AND owner:{}".format(sd_fs_name, user), item_type="Service Definition")[0] print("Found SD: {}, ID: {} \n Uploading and overwriting...".format(sdItem.title, sdItem.id)) sdItem.update(data=sd) print("Overwriting existing feature service...") fs = sdItem.publish(overwrite=True) if shrOrg or shrEveryone or shrGroups: print("Setting sharing options...") fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups) print("Finished updating: {} - ID: {}".format(fs.title, fs.id))
You can run the code from command line with the following command.
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe c:\mycode\updatewebmap.py
Once you’re sure the script has properly updated your hosted feature service, you can use the command in a scheduled task. This blog has more information about creating a scheduled task.
Important Notes:
The Python script needs to be run on a machine with a valid ArcGIS Pro license. The script uses arcpy to create and stage the data to be published.
The Python script assumes the first map found within a project will be the one to publish. If you’re working in a project with more than one map, update the Python code or create a new project with only the map you wish to share.
The associated service definition (.sd) file must exist on ArcGIS online, along with the hosted feature layers and web map. If either of these items are missing, you’ll need to publish a web map from ArcGIS Pro independent of this script before implementing the update workflow.
The ArcGIS API for Python provides many options and settings for working with your hosted content. Explore the options on publish and content for more fine grain control on your update process.
from ArcGIS Blog http://ift.tt/2mo1y67
0 notes