5

Is it possible to have the python shell from either qgis or argis to load (import) both qgis.core and arcpy?

I am developing a metadata creation tool and have completed the core functionality using arcpy for formats that arcgis reads but want it to handle .tab files as well without having to convert the files to shp and then analyse them as in this post How to read MapInfo .tab files in ArcPy?

=== Thanks to RK I got some of this working (see code below)

BUT I can't figure out how to get the projection and other important info...http://www.gis.usu.edu/~chrisg/python/2009/docs/ogr.pdf includes a GetSpatialReference, GetGeometryType( self ) and GetGeometryName( self ):but how do you use them?

import osgeo, os
from osgeo import ogr
os.chdir(r'L:\\Data_Admin\\QA\\Metadata_python_toolset\\test\\Local_Govt\\TRC\\TAB')
os.getcwd()

driver = ogr.GetDriverByName('MapInfo File')
fn='trc_boundary.tab'
dataSource=driver.Open(fn,0)
layer = dataSource.GetLayer(0)
feature = layer.GetFeature(0)
extent=layer.GetExtent()
numFeatures=layer.GetFeatureCount()
#id = feature.GetFieldAsString('id') # this didn't work
geometry = feature.GetGeometryRef()
x=geometry.GetX()


#delete datasets from memory
#feature.Destroy()
#dataSource.Destroy()

And is there a tool similar to arcpy.ListFeatureClasses() if not I guess we can use glob as anyway for ogr the driver has to be specified.

1
  • You might want to change the question ;-)
    – R.K.
    Commented Feb 2, 2012 at 10:31

2 Answers 2

3

Going with OGR and GDAL as you did is probably the right way. You've got a solution that works.

Concerning the original question «Is it possible to have the python shell from either qgis or argis to load (import) both qgis.core and arcpy?», the answer is not that simple :

  • Technically Yes, you can
  • Legally, you can't (mostly)

QGIS is GPL for its C++ core and gui parts, and QGIS Python bindings are GPL too.

The general agreement, which has been confirmed by the FSF and a law study by the Plone project, is that when you import a Python GPL module, the whole code becomes GPL, as it's considered a link. This is the main statement, but it can be a bit more complicated in the details. However, unless you really want to mess with lawyers, you should stay with the initial statement.

Note that nothing requires however that you distribute your product. But if you do, you'll have to provide the sources.

See : http://www.gnu.org/licenses/gpl-faq.html#IfInterpreterIsGPL

However, when the interpreter is extended to provide “bindings” to other facilities (often, but not necessarily, libraries), the interpreted program is effectively linked to the facilities it uses through these bindings. So if these facilities are released under the GPL, the interpreted program that uses them must be released in a GPL-compatible way.

http://plone.org/foundation/copyrights/license-faq

Must add-on products for Plone be licensed under the GPL?

In most cases, yes.

The GPL covers any "derivative works" of Plone, and defines these derivative works as those which:

copy or modify the code we provide to you (this includes Python code, HTML, images, etc.)
links to our GPL'd Python code (eg, by using Python's "import" statement)

The vast majority of add-on products exhibit one or both of these behaviors, and, as such, must be licensed under the GPL.

It is possible to create an add-on product that does not exhibit these behaviors (many generic Zope products that are not specific to Plone, and some Plone themes, do not). Such products need not be licensed under the GPL.

3

It should be possible as long as both modules are visible to the Python shell you'd use. You might need to add one or both modules to the PYTHONPATH variable.

However if all you want is to read tab files without converting, you're better off using gdal/ogr. It should be able to open the tab files and get the metadata you want.


I hope this works.

import osgeo, os
from osgeo import ogr
os.chdir(r'L:\\Data_Admin\\QA\\Metadata_python_toolset\\test\\Local_Govt\\TRC\\TAB')
os.getcwd()

fn='trc_boundary.tab'
dataSource=osgeo.ogr.Open(fn)
layer = dataSource.GetLayer(0)
projection = layer.GetSpatialRef() #Get spatial reference
feature = layer.GetFeature(0)
extent=layer.GetExtent()
numFeatures=layer.GetFeatureCount()
#id = feature.GetFieldAsString('id') # this didn't work
geometry = feature.GetGeometryRef()
x=geometry.GetX()    

geomtype = geometry.GetGeometryType() #Get geometry type
geomname =  geometry.GetGeometryName() #Get geometry name

##To list the features:
for i in range(layer.GetFeatureCount()):
    feature = layer.GetFeature(i)
    name = feature.GetField("id")
    geometry = feature.GetGeometryRef()
    print i, name, geometry.GetGeometryName()
9
  • I do have it as C:\Program Files (x86)\Quantum GIS Wroclaw; C:\Program Files (x86)\Quantum GIS Wroclaw\apps\Python25; C:\Python26\ArcGIS10.0; C:\Python26\ArcGIS10.0\Scripts could it be since QGIS, ArcGIS and FWTOOLS install their own copy of python?
    – GeorgeC
    Commented Feb 2, 2012 at 3:13
  • Yeah. Have you tried importing qgis core from your ArcGIS python shell?
    – R.K.
    Commented Feb 2, 2012 at 3:16
  • I just noticed that you are using a QGIS version that uses Python 2.5. ArcGIS uses 2.6. You might want to upgrade to a QGIS version that uses Python 2.6 to minimize cross version incompatibilities.
    – R.K.
    Commented Feb 2, 2012 at 3:27
  • yup, tried both...It's best to get qgis.core/QtPy working in the arcgis install of python as arcpy won't work in the qgis shell. My PYTHONPATH=C:\Program Files (x86)\Quantum GIS Wroclaw; C:\Program Files (x86)\Quantum GIS Wroclaw\apps\Python25; C:\Python26\ArcGIS10.0; C:\Python26\ArcGIS10.0\Scripts; C:\Program Files (x86)\Quantum GIS Wroclaw\apps\qgis\bin; C:\Program Files (x86)\Quantum GIS Wroclaw\bin
    – GeorgeC
    Commented Feb 2, 2012 at 3:32
  • So you can use both libraries now as long as you use the ArcGIS Python shell?
    – R.K.
    Commented Feb 2, 2012 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.