Replacing arcpy.AddMessage() with open source?

Replacing arcpy.AddMessage() with open source?

I've recently inherited some old python scripts that contained ArcPy modules. I'm using version 10.0.

I am looking for an error messaging system that replaces arcpy.AddMessage()?

I assume you are trying to convert your scripts so that they can be used in open source software or with open source libraries, otherwise the question's title would not make too much sense to me.

First of all you have to be aware that arcpy.AddMessage solely makes sense in one specific context: when used in a script tool that is used within ArcGIS. It does not much more than a print statement, but it prints it to the tool's interface instead of your console. So, if you are trying to convert your scripts using open source, then you would just have to replace these parts with a simple print statement (if running from the console or an IDE, if you are using a GUI library such as Tkinter or PyQt, then you would have to use different commands to display messages in your interface).

If you are addressing errors specifically then you would use your statement in a try/catch block or in a conditional statement (that addresses whatever fails).

Should you stick with Esri, then you could also use the arcpy.AddError command, but you have to be aware that unlike addMessage or addWarning this command will end your tool's execution the second it appears! Therefore, be very careful to only use it when an error truly occurs, or your tool will crash (even though there might not have been an error at all).

How to check if an actual map document is loaded / opened up in ArcMap

I'm working with C#, Visual Studio 2010 and ArcMap. I have a fairly simple question that I am not sure of the answer because of my Noob status with ArcObjects and ArcMap.

I am familiar with many of the ArcObjects Interfaces such as IMxDocument , IMap , IActiveView and IPageLayout .

I was wondering how to check if an actual map document is loaded into ArcMap or opened up in ArcMap. I am working on a boolean function on a button click. I am just not sure what interfaces or ArcObjects (if any) would be needed to return True if there is a map document loaded or not.

2 Answers 2

As an alternative to Jason's answer if you wanted to stick with os.walk then I normally do the following to find CSV files and then work with them

That way you can just use the fileList to work on

Oh, I just saw your problem. You are joining the entire list files to create your full_path variable. If you don't need to dig down into subdirectories where your csv files are located I might use the glob module. I've also used the rawstring character r this little guy is very helpful in that it will allow you to copy and paste your paths without having to add another backslash for each directory or change them to forward slashes:

Project 2: Batch reprojection tool for vector datasets

Some GIS departments have determined a single, standard projection in which to maintain their source data. The raw datasets, however, can be obtained from third parties in other projections. These datasets then need to be reprojected into the department's standard projection. Batch reprojection, or the reprojection of many datasets at once, is a task well suited to scripting.

In this project, you'll practice Python fundamentals by writing a script that re-projects the vector datasets in a folder. From this script, you will then create a script tool that can easily be shared with others.

The tool you will write should look like the image below. It has two input parameters and no output parameters. The two input parameters are:

  1. A folder on disk containing vector datasets to be re-projected.
  2. The path to a vector dataset whose spatial reference will be used in the re-projection. For example, if you want to re-project into NAD 1983 UTM Zone 10, you would browse to some vector dataset already in NAD 1983 UTM Zone 10. This could be one of the datasets in the folder you supplied in the first parameter, or it could exist elsewhere on disk.

Running the tool causes re-projected datasets to be placed on disk in the target folder.


To receive full credit, your script:

  • must re-project shapefile vector datasets in the folder to match the target dataset's projection
  • must append "_projected" to the end of each projected dataset name. For example: CityBoundaries_projected.shp
  • must skip projecting any datasets that are already in the target projection
  • must report a geoprocessing message telling which datasets were projected. In this message, the dataset names can be separated by spaces. In the message, do not include datasets that were skipped because they were already in the target projection. This must be a single message, not one message per projected dataset. Notice an example of this type of custom message below in the line "Projected . . . :"

Successful completion of the above requirements is sufficient to earn 90% of the credit on this project. The remaining 10% is reserved for "over and above" efforts which could include, but are not limited to, the following:

  • Your geoprocessing message of projected datasets contains commas between the dataset names, with no extra "trailing" comma at the end.
  • User help is provided for your script tool. This means that when you open the tool dialog and hover the mouse over the "i" icon next to each parameter, help appears in a popup box. The ArcGIS Pro Help can teach you how to do this.

You are not required to handle datum transformations in this script. It is assumed that each dataset in the folder uses the same datum, although the datasets may be in different projections. Handling transformations would cause you to have to add an additional parameter in the Project tool and would make your script more complicated than you would probably like for this assignment.

Sample data

The Lesson 2 data folder contains a set of vector shapefiles for you to work with when completing this project (delete any subfolders in your Lesson 2 data folder—you may have one called PracticeData—before beginning this project). These shapefiles were obtained from the Washington State Department of Transportation GeoData Distribution Catalog, and they represent various geographic features around Washington state. For the purpose of this project, I have put these datasets in various projections. These projections share the same datum (NAD 83) so that you do not have to deal with datum transformations.

The datasets and their original projections are:

  • CityBoundaries and StateRoutes - NAD_1983_StatePlane_Washington_South_FIPS_4602
  • CountyLines - NAD_1983_UTM_Zone_10N
  • Ferries - USA_Contiguous_Lambert_Conformal_Conic
  • PopulatedPlaces - GCS_NorthAmerican_1983


Deliverables for this project are as follows:

  • the source .py file containing your script
  • the .tbx file containing your script tool
  • a short writeup (about 300 words) describing how you approached the project, how you successfully dealt with any roadblocks, and what you learned along the way. You should include which requirements you met, or failed to meet. If you added some of the "over and above" efforts, please point these out so the grader can look for them.

The following tips can help improve your possibility of success with this project:

    Do not use the Esri Batch Project tool in this project. In essence, you're required to make your own variation of a batch project tool in this project by running the Project tool inside a loop. Your tool will be easier to use because it's customized to the task at hand.

There are a lot of ways to insert "_projected" in the name of a dataset, but you might find it useful to start by temporarily removing ".shp" and adding it back on later. To make your code work for both a shapefile (which has the extension .shp) and a feature class in a geodatabase (which does not have the extension .shp), you can use the following:

In the above code, fc is your feature class name. If it is the name of a shapefile it will include the .shp . The replace function searches for any string ".shp" (the first parameter) in the file name and replaces it with nothing (symbolized in the second parameter by empty quotes ""). So after running this code, variable rootName will contain the name of the feature class name without the ".shp" . Since replace(. ) does not change anything if the string given as the first parameter does not occur in fc, the code above can be replaced by just a single line:

You could also potentially chop off the last four characters using something like

but hard-coding numbers other than 0 or 1 in your script can make the code less readable for someone else. Seeing a function like replace is a lot easier for someone to interpret than seeing -4 and trying to figure out why that number was chosen. You should therefore use replace(. ) in your solution instead.

First thing is that your function fileExists can be replaced with os.path.isfile function

Now here you cannot have negative size of string so to make it less confusing you can do just:

however I would not even create a function for this, because empty string is a False in boolean meaning so this:

which can be prettifed using filter function:

Now speaking about opening files, it is better to use with statement, it is safer, prettier and more pythonic way. So this:

Another thing is that python functions/variables should named using underscore as separtor. So in the end what we have is:

I see two good strategies to accomplish the task.

One solution is to read all of the text into a list, then rewind to the start of the file and write out the desired lines.

The disadvantage of that approach is that it scales poorly if the file is huge, because the entire contents have to be read into memory first. The other approach would be to overwrite the file while reading from it. This uses two file objects, each keeping track of its own position within the file.

Note some other issues with your code.

First, PEP 8, the official Python style guide, recommends lower_case_with_underscore for function names, and two blank lines between functions.

The fileExists function does not just test for the existence of the file — it actually checks whether you can open it for reading, which is a more stringent condition, since it also involves file permissions. But I don't see any reason to check specifically for file existence. All kinds of I/O errors are possible, such as filesystem permission denial, read-only filesystem, disk quota, or hardware failure. Furthermore, even if the file exists when you check, it could disappear during the split second between if not fileExists(…) and the real open(…) call. On top of that, your rmoveEmptyLines function has no way of reporting failure to its caller. (Printing an error message doesn't count!) Therefore, the only reasonable approach is to Just Do It, and handle any exception that might occur.

Any open() call should be written using a with block, which will automatically close the file handle when exiting the block.

Python scripting: using feature classes with GP services

I was working on a geoprocessing (GP) service that could allow a user to specify what feature class in a registered geodatabase on the server a GP task will work with. I’ve tested using the Truncate Table tool first to basically wipe all the rows from a feature class while preserving the schema.

After creating a simple model with just one tool and one model parameter (Input Table), I’ve run it successfully and have started the publishing process. Yet when publishing a model as a GP service, one would have three choices: “User defined value” (user specify value for the parameter – for instance, what features will be used as a FeatureSet), “Choice list” (this one gets created when running the model in ArcMap and having several layers added to the Table Of Contents), and finally “Constant value” (the same input data will be used for the parameter). The only applicable choice in my scenario is to use a user defined value when users specify what feature class in the geodatabase will be used. Since new feature classes will be created, the choice list option cannot be used. Using a constant value is not an option either since I want to let a user specify a feature class dynamically rather than let them run the GP task over the same feature class all the time.

However, FeatureClass is not a supported input type for GP Services which makes it impossible to publish and use my model directly. This means that I’ll have to implement a look-up logic and pass only a string to the service instead of the feature class itself. Such string would represent a feature class name. So, what I needed to do is to obtain a feature class name from a user, and then use the logic to append the name of the feature class to the geodatabase path. GP service will then find the feature class in a specified beforehand geodatabase, and run the Truncate Table tool.

I have had hard times making this possible in ModelBuilder by using the inline variable substitution for the input table path, so I’ve decided to completely switch to Python. I have created a script tool which asks for an input feature class name and then use this string in the data path which GP service can work with. The code is as below:

Rate this:

DEV203x Introduction to Bootstrap: Uh-oh

I was very excited to see that Microsoft is now offering MOOCs through edX. I immediately signed up for Introduction to Bootstrap and Transact-SQL. Bootstrap opened up on 1 April, so I spent some time getting the videos, downloading the notes and preparing my OneNote section for it.

I idly clicked over to the first lab, which looked promising – a detailed assignment, clearly designed to test the knowledge gained from the videos and notes, with clear submission rules. I then scrolled to the bottom and saw this:

I’ve mentioned in several previous posts that peer assessments in a MOOC is a dealbreaker for me. I’ve changed my opinion somewhat on the use of video lectures (just barely though), but making peer assessments a requirement is a bit much for me.

While my need to finish what I’ve started is strong, I have many other things to do as well, so for this one I may be content to just grab the content for self-paced learning/reference at a later stage.

Display subtypes and their descriptions for a feature class

I posted this gist earlier today to check the subtypes that are available in a given feature class.

import arcpy
ftr = r"C://Some//Arb//Folder//work.gdb//ftr"
subs = arcpy . da . ListSubtypes ( ftr )
for k , v in subs . iteritems ():
print k , v [ 'Name' ]

It’s quite self-explanatory, so I posted it as-is, as a useful code snippet. The subtype codes are returned as keys in the dictionary, while the values are themselves dictionaries containing all the properties of the subtype.

I’m only interested in the description of the subtype, which is why I am printing out the value in the values dictionary at the key ‘Name’. Full description of the function capabilities is here.

On an unrelated note, I can’t get used to the new structure of the online help.

Cary Chadwick

1:35pm EDT

2:15pm EDT

AUTHORS: Paula Kay Lazrus, St. John's University

ABSTRACT: The idea of creating an opportunity for students learning GIS for the first time to utilize their newly acquired skills is both appealing and challenging. In collaboration with the New York Botanical Gardens a project was developed that would allow students to apply skills they were still mastering to the production of a tangible product. Pairs of students explored a particular plant and its distribution in the NYC area and then created both map layouts and a final story map of their project. The students involved in the project had no prior GIS experience and often no access to GIS software outside of institutional labs. The challenges included basic acquisition of sufficient skills in the time frame necessary to complete the project as well as issues of comprehending fundamental issues connected to the questions they were asked to address.

2:15pm EDT

AUTHORS: Keith Ratner, Salem State Glenn Hazelton, Northeastern University

ABSTRACT: Where GIS is being taught in Higher Ed, In the Geography world it has been noticed that the discipline is losing GIS to other areas of study. Where is it migrating to? What does this mean for Geography? Keith Ratner will present the view from a Geography perspective. Glenn Hazelton will discuss the range of students he sees in his classes. Together we hope to generate an interesting discussion. Out joint goal is to generate a discussion about developing a coordinated interdisciplinary approach to educating people about GIS and the science of geospatial technologies.

2:15pm EDT

AUTHORS: Lyn Malone, World Views GIS

ABSTRACT: Participants in this workshop will learn how to work with ArcGIS Online, StoryMaps, mobile data collection apps such as Collector for ArcGIS and Survey123, and other historical geospatial resources. As part of this workshop, participants will begin to think about developing a lesson or exercise that can be used in one of their courses. This workshop is strongly encouraged for high school social studies teachers and for college and university social science faculty.

2:45pm EDT

AUTHORS: Shane Bradt, UNH Cooperative Extension

ABSTRACT: While geospatial technologies have been taught as a topic and employed in outreach at UNH Cooperative Extension for nearly two decades, the recent increased use of ArcGIS Online and related applications have made substantial improvements to outreach programs in New Hampshire. Once focused on desktop GIS and standard GPS units, the Geospatial Technologies Training Center now teaches a variety of workshops on online and mobile mapping technologies, including ArcGIS Online, Story Maps, crowdsource mapping, Survey123 and Collector. In addition to helping the NH public learn how to put these technologies to use in their lives, UNH Cooperative Extension has begun incorporating ArcGIS Online-base approaches across a variety of outreach programs, as well as, for internal communication and collaboration. While some applications of ArcGIS Online have served simply as a way to improve web-based communication with the public, others have deeply changed the way in which programming is delivered. In these cases, not only has ArcGIS Online allowed UNH Cooperative Extension to create new programming, it has allowed the organization to reach new audiences. Come join this session to learn how UNH Cooperative Extension is using ArcGIS Online for outreach and share examples of your own outreach using online mapping.

2:45pm EDT

AUTHORS: Mike Wagner, Loudon County Public Schools

ABSTRACT: The Geospatial Semester is a unique partnership between high schools in Virginia and the Integrated Science and Technology department at James Madison University (JMU). High school seniors participating in the Geospatial Semester take classes on geospatial technology in their home school and can earn credit from JMU. A key aspect of the program is a focus on local projects connecting students, technology, and their community. Mike Wagner, lead teacher for GIS in the Loudon County Public Schools, will share his county’ implementation model for this exciting program.

3:30pm EDT

How do you teach students to use GIS and other geospatial technologies? What has worked best for you and what advice do you have for beginners?

Round Table Exchange Networks
The final sessions of the day provide opportunities for attendees to network with others about critical topics in education and GIS. The goal of these sessions is to share successful ideas, resources, projects, and teaching strategies. The sessions will be facilitated by discussion leaders, but the discussion content will be totally shaped by participants. Round Table discussions will be summarized in the final wrap-up session.

3:30pm EDT

How have you used GIS and other geospatial technologies to implement your own curriculum?

Round Table Exchange Networks
The final sessions of the day provide opportunities for attendees to network with others about critical topics in education and GIS. The goal of these sessions is to share successful ideas, resources, projects, and teaching strategies. The sessions will be facilitated by discussion leaders, but the discussion content will be totally shaped by participants. Round Table discussions will be summarized in the final wrap-up session.

3:30pm EDT

What do you value most and least about GIS Educators Day as it is currently configured? What else would you like to see included?

Round Table Exchange Networks
The final sessions of the day provide opportunities for attendees to network with others about critical topics in education and GIS. The goal of these sessions is to share successful ideas, resources, projects, and teaching strategies. The sessions will be facilitated by discussion leaders, but the discussion content will be totally shaped by participants. Round Table discussions will be summarized in the final wrap-up session.

4:15pm EDT

5:00pm EDT

7:30am EDT

7:30am EDT

7:30am EDT

8:00am EDT

8:45am EDT

Sunday, July 15, 2018

Location Decisions

Looking for the ideal place to live can be tricky when there are several factors that you want to consider. This week in Applications in GIS we were tasked with helping a fictitious couple find an ideal area to live near the University of Florida and North Florida Regional Medical Center (their places of employment). Both places of employment are in the city of Gainesville, FL located in Alachua County. Besides considering daily commute for both parties, the couple wants to live in an area where there are people close to their age group (40 to 49) and a majority of homeowners.

To begin the analysis, raster data was compiled to show the Euclidean distance surrounding both UF and NFRMC. Then, additional raster data was created from census information to determine where individuals in the same age group as the couple resided as well as where the majority of homeowners lived in the area. Each raster set was reclassified accordingly to be used in a weighted analysis later. All of the data needed to be in the same format to be used in the weighted analysis tool appropriately and so each dataset needed to be reclassified.

Below are my maps for this week. The first map shows all of the data individually in separate maps. There are census tracts highlighted in each to show potential areas to live if only that data were being considered. The second map contains the results of the weighted analysis. Originally, the couple wanted all four of their requests to be considered equally in the analysis, but after seeing traffic in the area decided that more emphasis should be placed on location in relation to work. The first frame in the second map depicts the results of weighing the distance from UF and NFRMC at 30% and the census data considerations to 20%. The second frame depicts the results of weighing each factor equally at 25%. I decided to only change the percentages slightly for the distance from work since overtime travel conditions can change with the implementation of new road construction or public transportation. Lifestyle considerations in an area tend to change more slowly, or jobs may change, and so I decided not to place too much emphasis on commute time but did increase it as requested.

Watch the video: Whats new in the ArcGIS API for JavaScript Release!