Hot answers tagged python27
6
>>>'R12345678910'[1:]
'12345678910'
Edit
Appending [1:] to a string in python will remove the initial character. Look up slice in the python help.
To use this in the ArcGIS field calculator, you will need to turn on Python parsing
for the unitCode = enter
!your_field_name![1:]
4
Did you check this page? It may give you some idea how to set it up:
http://grasswiki.osgeo.org/wiki/GRASS_and_Python
-> Creating Python scripts that call GRASS functionality from outside
--> MS-Windows
4
VBScript Example:
Right( [FieldName], 11 )
This code will take the 11 characters from the right, and anything left of those 11 characters is omitted.
Python Example:
!FieldName!.lstrip('R')
This code takes a string and removes R from the left/front of it.
Both of these examples should get rid of the R in your strings.
Edit: Regan Sarwas' updated ...
4
Under the hood QGIS uses GDAL/OGR for most of the functions. So, the Python API for GDAL would be the closest analogy to using ArcPY in a stand-alone situation.
You can use the installation that comes with QGIS or have a separate installation of Python and GDAL. Other addons that complete the 'package' I would include
Shapely
Numpy
SciPy
The last ...
3
See the OGR Projections tutorial and the OGRSpatialReference class. In particular, the GetAttrValue method.
Here's a worked example.
from osgeo import gdal,osr
ds=gdal.Open(r'SOMERASTER.TIF')
prj=ds.GetProjection()
print prj
srs=osr.SpatialReference(wkt=prj)
if srs.IsProjected:
print srs.GetAttrValue('projcs')
print srs.GetAttrValue('geogcs')
For my ...
3
The syntax is
Dissolve_management (in_features, out_feature_class, {dissolve_field}, {statistics_fields}, {multi_part}, {unsplit_lines})
You need to specify the dissolve_field as the third parameter - here you've left it blank.
3
If an unhandled exception, such as an ImportError, occurs before the add-in classes are instantiated they will become unresponsive, be given a [Missing] label, and have a red symbol for their icon in the case of items on toolbars or in menus:
You can confirm whether an import error is happening by wrapping your import statement with an exception handler ...
3
There are a couple of ways to do this. The quickest would be to read the file as you have done and then use the string split function.
b = open("test.txt", "r").readlines()
line = b[-2].split() #Gets the second last value from the list and split on whitespace
return float(line[-1])
However this isn't very reusable. If you're doing this for lots of similar ...
3
I think it's because GDAL wasn't able to create a layer from the shape file. The offending line:
out_shp = '%s\test_OGR.shp' % (path,)
You have to escape the slash, like so:
out_shp = '%s\ \test_OGR.shp' % (path,)
Otherwise, Python would will treat the \test part as \t or tab. Your resulting path would then be:
D:_tempAuto est_OGR.shp
...
2
Here is code snippet for people who would like to use it:
import arcpy
import pythonaddins
class DrawRectangle(object):
"""Implementation for rectangle_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 1
self.shape = 'Rectangle'
def onRectangle(self, rectangle_geometry):
extent = ...
2
OGR is unable to open your dataset. Check that the dataset exists, the path is correct and that you have a version of GDAL/OGR with GRASS vector support as it is not compiled by default.
To check if you have GRASS vector support, type ogrinfo --formats at a command prompt.
2
Yes building a standalone application out of QGIS using Python and the QGIS libraries is possible.
I have some small, but hopefully growing, example set at https://github.com/NathanW2/pyqgis-playground
The main thing is having the path the libraries setup correctly. When I'm on windows I use something like this:
@ECHO OFF
set ...
1
@NathanW's suggestion works for me and is what I would suggest doing as well.
I have in the Install folder within my Python Add-in directory:
child.py:
import os, datetime
def writeDummyFile(path):
with open(path, "w") as f:
f.write(datetime.datetime.now().ctime())
if __name__ == "__main__":
writeDummyFile(r"C:\temp\test.txt")
...
1
This should work; it worked for me. This will give you a group layer named by the folder and a layer with the name of the shapefile. I built off what artwork21 posted.
import os
import arcpy
WS = r'c:\test_define'
arcpy.env.workspace = "in_memory"
mxd = arcpy.mapping.MapDocument(r"c:\test_define\test_python_add2.mxd")
df = ...
1
use the QGIS version of Kyng Chaos and no more problem:
it uses the Apple Python (2.6 in Snow Leopard, 2.7 in the Lions's)
it does not install anything that is already installed on Mac OS X (Python, etc.), like MacPorts.
there are no problems of PATH:
In addition:
the Path problems arise because the original build script of QGIS is made for the Apple ...
1
your out_dataset points to a gdb, not a feature classs or table as required.
you could try this, which should give each table in the output database the same name it had in the input database:
out_dataset = r"E:\Database Project\ACS Estimate 2011\Data\To Database\Seq Tables.gdb" + "\\" + table
edit: forgot to make it a raw string
1
The problem is that the out_dataset needs to point to a feature class table. Right now you have it pointed just to the geodatabase.
This should work (work path slashes are going the wrong way '\' need '/'):
import arcpy
from arcpy import env
env.workspace = "E:/Database Project/ACS Estimate 2011/ACS 2011 Tables.mdb"
tableList = arcpy.ListTables()
for ...
1
Eventually I managed to have the personal shell launcher!
The trick was to investigate the "o4w_env.bat" file in order to understand what the "call" at the second line actually does.
So I tried and succeded with this:
Copy the "idle.pyw" (or the IDE You'd like to use to the Quantum GIS main folder (in my case it was "C:/Quantum_GIS_Lisboa");
Copy the SHELL ...
1
It looks like you have a similar problem to one I had. Though the specifics of the paths are different I believe that a similar solution can work. See my solution in this post.
What is happening is that it is not just the PYTHONPATH which is read but also the PATH variable. On load QGIS appends the paths of the osgeo binaries to the path, which means they ...
1
The first trivial optimisation that comes to mind is storing pointFeature.GetGeometryRef() in a temporary variable instead of repeating the lookup for each inner loop iteration.
But that is likely completely insignificant. Since you want just the distance to the nearest polygon, you don't need two loops. Make a union out of all the polygons / dissolve the ...
1
When configuring python for grass I faced with same issue. I've found grassrc6 under APPDATA:
It doesn't matter what the file is called, so long as %GISRC% points to it and it contains the necessary settings.
The normal location for GRASS 6.x on Windows is:
%APPDATA%\GRASS6\grassrc6
On Windows 7, a typical setting for %APPDATA% is
...
1
A hackish workaround would be to create a symlink with that username to your own home dir. But it seems either ui_teste.py or ui_teste.ui has that path hardcoded, so check them out (or search the whole hierarchy where they are for "kyngchaos"). There's also a chance the pyqt4 package you installed is buggy, but first try the previous suggestion.
1
Did you upgrade from a previous version of QGIS, and have you installed extensions into that version?
I think the problem is incompatibility between the old extension and the new version of QGIS. Can you try renaming C:/Users/bruno.saphy/.qgis (to say, C:/Users/bruno.saphy/.qgis_original), then try running QGIS again?
Only top voted, non community-wiki answers of a minimum length are eligible


