2

I am writing an ArcPy script (ArcGIS Pro) to export tile in the area of interest with help of arcpy.ExportTileCache_management(). I am getting the area of interest from a table in PostGIS. As far as I read in the documentation are the area of interest must be Feature Set data type.

I tried FeatureSet.from_json to convert my table in PostGIS but it was not successful. I kept getting the following error

File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-90516b90cd60>", line 1, in <module>
    runfile('C:/Users/ncipa/PycharmProjects/arcpy/test.py', wdir='C:/Users/ncipa/PycharmProjects/arcpy')
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/ncipa/PycharmProjects/arcpy/test.py", line 518, in <module>
    areaofinterest = FeatureSet.from_json(polygon)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\feature.py", line 694, in from_json
    return FeatureSet.from_dict(json.loads(json_str))
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\json\__init__.py", line 348, in loads
    'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

How can I convert the record in my table in PostGIS to Feature Set?

# Connect and query tables in Postgresql
try:
    connection = psycopg2.connect(user='xxx',
                                  password='xxx',
                                  host="xxx",
                                  port="5432",
                                  database="xxx")
    cursor = connection.cursor()
# Fetch records
postgreSQL_select_Query1 = &quot;select pgnr,  ST_AsGeoJSON(geom) from xxx&quot;
cursor.execute(postgreSQL_select_Query1)
records = cursor.fetchall()


except (Exception, psycopg2.Error) as error: print("Error while connecting to PostgreSQL")

finally: # closing database connection. if (connection): cursor.close() connection.close() print("PostgreSQL connection is closed")

for i, row in enumerate(records):

polygon = json.loads(row[1])

cacheSource = r&quot;E:\xxx_cache&quot;
cacheTarget = r&quot;E:\_xx&quot;
cacheName = &quot;Target&quot;
cacheType = &quot;TILE_CACHE&quot;
storageFormat = &quot;Compact&quot;
scales = &quot;100000&quot;
areaofinterest = FeatureSet.from_json(polygon)

arcpy.ExportTileCache_management(cacheSource, cacheTarget, cacheName,
                                 cacheType, storageFormat, scales, areaofinterest)

if i == 0:
    break

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Nil
  • 1,517
  • 8
  • 22
  • 1
    Please [Edit] the question to specify the way in which is was "not successful". Your code snippet could be cut down to a single geometry, with legible contents. – Vince Dec 07 '20 at 12:26
  • I added the error message that I am receiving when I run the code snippet above. – Nil Dec 07 '20 at 13:06
  • What is this? FeatureSet.from_json ? arcpy has a FeatureSet -- but based on your code, I'm not sure if this is an arcpy FeatureSet or something else? – KHibma Dec 07 '20 at 13:06
  • I am writing an arcpy script for the first time and I do not have a good knowledge obviously. As it is written in the question, I wanted to use the 'Export Tile Cache' (https://pro.arcgis.com/en/pro-app/tool-reference/data-management/export-tile-cache.htm). I read in the documentation that the data type of AOI must be a Feature Set. Therefore, I went on searching how I can convert the records in my table and then I saw FeatureSet.from_json in the following link https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureSet.from_json and used it. – Nil Dec 07 '20 at 13:20
  • 2
    ArcGIS can natively read PostgreSQL/PostGIS tables as feature classes (I do this 10-12 hours/day), So using psycopg2 to do this seems the long way around the lake. – Vince Dec 07 '20 at 15:43
  • Thanks for the tip! I have used "Create Database Connection" to create a native connection as you suggested. But I still can not get it as feature classes. I am following this https://desktop.arcgis.com/de/arcmap/10.3/analyze/arcpy-classes/arcsdesqlexecute.htm – Nil Dec 07 '20 at 16:48
  • 1
    Ah my ignorance! egdb = r"E:\xxxx.sde\db_name.schema.table_name" >> It gives me the feature class!!! – Nil Dec 07 '20 at 16:59

1 Answers1

3

There are a few things here that might be causing your problem.

  1. I assume this is an actual arcpy FeatureSet: areaofinterest = FeatureSet.from_json(polygon) The proper way to "put something" into a featureset is via load. Documentation reference That said, I'm not entirely sure what it is you're getting out of your postgres connection. Its very possible that output simply cannot be pumped into a feature set. It may require some transformation. I just don't know. Or maybe you can use FeatureSet.load(), and it'll just work. If that's the case, you're done.

  2. Input tool parameters can sometimes be confusing. You're right, that yes, the area of interest parameter requires a feature set as input. However, in the Esri-GP-Tool world, a feature set can be multiple things. The feature set parameter is a subset of feature class. It's used because when you're in the app (ArcMap / Pro) it allows one to draw features directly on the map right before they run the tool. This allows a user to skip the process of going and making a brand new featureclass and digitizing a shape. So, you could provide a featureclass as input into this parameter instead of an arcpy featureset.

Suggestion -- (this is based on what I think you can get out of your postgres connection) Throw the feature set idea away, just forget about it completely. Try to figure out how to get the postgres connection >> data (layer) into a "esri featureclass". Apologies, I can't offer a true solution as I'm not familiar with the psycopg2 module and what it provides. There are some hits here on GIS.SE that may provide some start points.

KHibma
  • 16,786
  • 1
  • 31
  • 55
  • The output of my postgres connection is a list and each record in the table is a tuple in the list. I have already been looking into PostGIS into an "esri featureclass" option. As a worst-case scenario, I will export my table in PostGIS into a shapefile via ogr2ogr and continue from there. – Nil Dec 07 '20 at 15:43