Tag Info

Hot answers tagged

8

You can call the GP tools in two ways: arcpy.%toolbox%.%toolname% or arcpy.%toolname%_%toolbox% Both are calling the same function, so there is no difference. It is a matter of taste; I always call functions in the arcpy.Buffer_analysis format because I seem to read the name tool faster in this way (I see first the toolname, and often seeing the ...


5

You can do this using 2 os.path functions (splitext and basename): import os os.path.splitext(os.path.basename(r'C:\user\shapefiles\polygon.shp'))[0] # returns 'polygon'


4

In general, workflows that you would like to automate I would recommend first doing it manually. Once you have that logic understood (what tools to use when), then yoiu could create a model/python script. For this case here would be the general model workflow (assuming you are using ArcGIS): Use Make XY Event Layer tool to create the GIS layer Use Add ...


4

It works in the python window because JHJ is likely a layer in the map and therefore can be reference in your script as "JHJ". When run outside of Arcmap, you need to tell arcpy where to look. Here are just a few ways you can do this (untested, but it should give you a few ideas): 1) jhj = ...


4

You can use the python threading library to run a function periodically: I have used the following statements to remove a set of graphics after 3 seconds: from threading import Timer Timer( 3, self._clearGraphicLayer, ()).start() def _clearGraphicLayer(self): for graphic in self.graphicsLayer: self.iface.mapCanvas().scene().removeItem(graphic) ...


4

Put a u in front of the string so the interpreter knows it's Unicode. myCalc(!Epulettipus!, u"Lakóépület")


4

Here is the code that should work for you: import arcpy mylist = ['A4126','A4190'] print str(tuple(mylist)) tempFeat_1 = r"C:\ArcGIS\Default.gdb\AR_postalcodes" tempFeat_2 = r"C:\ArcGIS\Default.gdb\AR_postalcodes_sel" qry = """POSTCODE IN {0}""".format(str(tuple(mylist))) arcpy.Select_analysis(tempFeat_1, tempFeat_2, qry) A couple of comments: ...


4

You need to open it as a QgsVectorLayer layer = QgsVectorLayer('/path/to/shapefile_folder/test.shp', 'test', 'ogr') QgsVectorLayer objects have a method called geometryType. If you call that method for your newly created layer : >>> print layer.geometryType() 2 Where 0 is points, 1 is lines and 2 is polygons


4

Finally found the proper way of running processing algorithms in PyQGIS standalone scripts. This answer is based on answers to Problem with import qgis.core when writing a stand-alone Python script that uses QGIS and to Error: Algorithm not found, which is in turn based on a Qgis-dev mailing-list discussion. I suggest you to follow the work flow given in ...


4

You could build a topology in a geodatabase, and then when you validate that topology, it will add vertices in the manner you describe. http://resources.arcgis.com/en/help/main/10.2/index.html#/Designing_a_geodatabase_topology/006200000004000000/ If you are not familiar with this, basically what you would do is create a feature dataset in a geodatabase, ...


4

It is much more intuitive, in my opinion, to work with Cursors (rather than trying to emulate the field calculator in a script) for this type of problem. This is how you would port the problem over to an Update Cursor: import arcpy # The input FC fc = "C:/W/Sik.gdb/yourFC" with arcpy.da.UpdateCursor(fc, ["aspect", "aspect_m60"]) as cursor: for row in ...


3

As you suspect arcpy.analysis.Buffer and arcpy.Buffer_analysis are two equivalent ways to run the same tool.


3

The attached (untested) script takes the name of a feature class, splits it by "_" and uses that as a basename for the join operations. The general idea is to use: basename = fc.split("_")[0] which converts, for example, abc_clip_diss to abc Then, you can use that basename to create new variables with os.path.join(): inFeatures = os.path.join(ws, ...


3

This look like there is a null geometry somewhere. To solve this, you should first run the "repair" tool. To avoid you code to crash (as you said that it works for the first polygons), you can add some testing if point: print ("{0}, {1}".format(point.X,point.Y)) else: print "No point"


3

The (optional) is added to the interface automatically when you specify that the parameter is optional so it really can't be avoided. You can set all fields as required but default the values to something: Then in the tool if the value is default or nonsensical (not found in file system for example): import arcpy, os, sys Oparam = sys.argv[1] if ...


3

I would use the "SHAPE@" token with the search cursor, then do something like this: with arcpy.da.SearchCursor(fc, ("SHAPE@", "UNIQUE_ID")) as searchCur: for row in searchCur: myDF.extent = row[0].extent myDF.scale = 50 arcpy.RefreshActiveView() arcpy.mapping.ExportToPDF(thisMap, ...


3

You don't 'Dim' variables in python, you just declare and assign them. That said, your basic route is to set the parser to python and check Show Codeblock. In the Pre-Logic Script Code box, enter your function like this: def DoThis(fld): val = 0 if fld <> 'a certain string': val = # do your calculation here return val In your ...


3

I guess the "proper" way is to use QGIS' API which I'd hope would expose that. But I don't know how to do that. An alternative is to look without the shapefile itself. The basics of the specification are on Wikipedia: https://en.wikipedia.org/wiki/Shapefile#Shapefile_shape_format_.28.shp.29 What you want is: bytes 32–35 of the main header which dictates ...


3

This answer is based on answers to Problem with import qgis.core when writing a stand-alone Python script that uses QGIS and to How to use QGIS explode lines with Python. I suggest you to follow the work flow given in Problem with import qgis.core when writing a stand-alone Python script that uses QGIS to enable your QGIS libraries in your OSGeo4W Shell. ...


3

I have written a short snippet code with simple syntax, so it will be easy to understand for a beginner. The flow is that you need to get the unique values in the Strings field and then find out whether there are more than one value in the Integers field for this String. If yes - then keep, if no (i.e., the only value was -1) - then delete the row. ...


3

There is an Esri blog post that describes the technique for doing this in detail called If you are stuck at "if" – Part 1: Part 1 – Gives examples of quick and dirty ways of using the Calculate Value tool to create branches using if statements in a model. In your case you want to check for the existence of a feature class rather than a product ...


3

I just tested (albeit at ArcGIS 10.3 for Desktop) to confirm that you can do this using the Integrate (Data Management) tool. This is available at all license levels and is a single quick step. The output is as below where the vertices are shown for the northern polygon but an extra vertex was inserted on the southern polygon too.


3

Use the "Any Value" data type instead of the "String" data type. I have no idea why multivalue strings don't work, but it's very counterintuitive...


3

Yes, you can run multiprocessing child processes from a toolbox script. Below is some code to demonstrate in a Python Toolbox (*.pyt). There are a number of "gotchas". Some (but not all) will be applicable to Python script tools in a binary toolbox (*.tbx), but I only use Python Toolboxes these days so have not tested. Some "gotchas"/tips: Make sure ...


3

Try this: import os, glob, shutil root_dir = "C:\Users\xxxx\Desktop\Test\\" country_dir = "Country_" grid_path = "C:\Users\xxxx\Desktop\Test\Grid\Grid.shp" # Get all files that constitute the Grid Shapefile gridShpFiles = glob.glob(grid_path[:-3]+"*") for path,dirname,files in os.walk(root_dir): if country_dir in path: for f in gridShpFiles: ...


3

This works for me. Are you trying to drag it from the Project Pane (either Folder or toolbox node) into Modelbuilder? Either make a connection to the folder the TBX lives in, or from the Toolbox node, right click and add toolbox pointing to your existing tool. If you're trying to drag from ArcMap to Pro, this wont work.


3

Your function is not returning anything. I've modified your code to return the value of aspect_m60. # Calculate Field import arcpy # Set environment settings arcpy.env.workspace = "c:/W/Sik" # Set local variables inTable = "Point" fieldName = "aspect_m60" expression = "getCalc(!aspect!)" codeblock = """def getCalc(aspect): if (aspect < 60): ...


2

QGIS can take a --code that you can pass a Python file to in order to run. qgis --code my myfile.py Run this command from the VBA shell command.


2

If I understand your question correctly, you can accomplish this without a custom tool. After you start an editing session, on the Editor Toolbar go to Editor>Options. On the Attributes tab, check Display the attributes dialog before storing new features, and then select the feature class you want to enforce this behavior on. Whenever a new feature is ...


2

Look at Using processing algorithms from the console import processing processing.alghelp("qgis:fieldcalculator") ALGORITHM: Field calculator INPUT_LAYER <ParameterVector> FIELD_NAME <ParameterString> FIELD_TYPE <ParameterSelection> FIELD_LENGTH <ParameterNumber> FIELD_PRECISION <ParameterNumber> NEW_FIELD ...



Only top voted, non community-wiki answers of a minimum length are eligible