|
POST
|
I haven't played around much with DictWriter. I usually use the following when I processing my AGOL data; it does show how I process the date/time data when given an epoch date. Code is just snippets, but it shows the basics. f = open('certification.xls','w')
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
features = json.loads(jsonResponse.read().decode("utf-8-sig").encode("utf-8"),
object_pairs_hook=collections.OrderedDict)[u'features']
# write header
f.write("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(
"OBJECTID",
...
"Date Certified",
...
"EditDate",
...
for feature in features:
# AGOL uses GMT/UTC - convert to local time
if feature['attributes']['CertDate'] is not None:
certTime = time.strftime('%c', time.localtime(feature['attributes']['CertDate']/1000))
else : certTime = None
editTime = time.strftime('%c', time.localtime(feature['attributes']['EditDate']/1000))
f.write("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(
feature['attributes']['OBJECTID'],
...
certTime if certTime is not None else "",
...
editTime,
...
... View more
02-22-2018
11:10 AM
|
1
|
9
|
3701
|
|
POST
|
Here is another version. It does not change the community names in feature2. It does update the reference code using the community names found in feature1. It will probably error out if there is a community name in feature2 that is not found in feature1. import arcpy
import itertools
# empty dictionary
citydict = {}
fields = ['fName', 'Ref']
fc1 = r'C:\Path\To\Test.gdb\Feature1'
fc2 = r'C:\Path\To\Test.gdb\Feature2'
# read fc1 fields into a dictionary { city: governorateCode }
citydict = {f[0]:f[1] for f in arcpy.da.SearchCursor(fc1,fields)}
# update governorate code in fc2 - no new cities are added to fc2
with arcpy.da.UpdateCursor(fc2, fields) as rows:
for row in rows:
cities = row[0].split(',') # split cities into a list
# search citydict to find matching governorate code
# note that cities not found in citydict will not have an associated code
# may error out if there is no city that links to citydict
found = [i for i in cities if i in citydict.keys()]
cityCode = []
for idx, val in enumerate(found):
city = ["".join(x) for _, x in itertools.groupby(str(citydict[val]), key=str.isdigit)]
if idx == 0:
codePrefix = city[0]
cityCode.append(city[1])
# note that the data in row[0] (city list) is not changed
row[1] = codePrefix+"_".join(cityCode)
rows.updateRow(row)
print "Done." If you are adding new communities to feature1, you will probably want a way to insert them into feature2. This script will not add new communities.
... View more
02-21-2018
08:48 PM
|
2
|
11
|
2647
|
|
POST
|
Translation: I have a layer of polygonal entities that I would like to automatically number but spiral. In order to have continuity by neighborhood. I am a beginner in python, thank you for helping me. Thank you Question: Do you want the spiral in a clockwise or counterclockwise direction? ( Voulez-vous la spirale dans le sens des aiguilles d'une montre ou dans le sens contraire?) Tagging https://community.esri.com/community/developers/gis-developers/python
... View more
02-21-2018
02:05 PM
|
1
|
3
|
5718
|
|
POST
|
I was able to run the script you sent without issue. I retrieved 41 global IDs: ObjectID: 1 GlobalID: {3D7FD4DB-8FD4-4150-A6FA-7C8D9F490028}
....
ObjectID: 41 GlobalID: {58DC4684-2657-468D-AF0E-85277746984D}
I also pasted the URL into a web browser and was able to examine the json. All global IDs were intact. Since you are using a URL to access the feature, my only suggestion would be to use something like urllib.urlencode to make sure any characters in the URL string that need special encoding are processed. The characters include = < >. But in these tests, it didn't seem to create a problem. I did wonder about the field names and case sensitivity, but again this did not seem to be an issue. Again, my tests were done using 10.5 and Python's 32 bit IDE 2.7.12
... View more
02-21-2018
11:16 AM
|
1
|
2
|
2186
|
|
POST
|
Following. I did some additional testing last night and still couldn't duplicate the issue with the global ID. I was going to suggest a test of your hosted feature service, and I would be willing to try that later today.
... View more
02-21-2018
09:49 AM
|
0
|
4
|
2186
|
|
POST
|
My previous code was to see if I understood your project. Table1 would be replaced by using a search cursor, and to save your data, instead of using table2 you would use an insert cursor. From your examples, it looks like you may want to use an update cursor. This would assume that at least one city (or one city code) is in each row of your second feature. The following code might serve as a starting point. It may need some tweaking or error checking code added depending upon the rules you are actually using. Richard Fairhurst has made a number of good suggestions for you to consider. import arcpy
import itertools
# empty dictionaries
myCodes = {}
myCities = {}
fields = ['fName', 'Ref'] # assumes same field names in both features
fc1 = r'C:\Path\To\Test.gdb\Feature1'
fc2 = r'C:\Path\To\Test.gdb\Feature2'
for row in arcpy.da.SearchCursor(fc1,fields):
# codes dictionary { governorate: [ city: numberCode ... ] }
item = ["".join(x) for _, x in itertools.groupby(str(row[1]), key=str.isdigit)]
if item[0] in myCodes:
myCodes[item[0]].append({row[0] : item[1]})
else:
myCodes[item[0]] = [{row[0] : item[1]}]
# cities dictionary { city: governorate }
if row[0] not in myCities:
myCities[row[0]] = item[0]
# print myCodes
# print myCities
with arcpy.da.UpdateCursor(fc2, fields) as rows:
for row in rows:
# find a city match in row[0]
cities = row[0].split(',')
found = [i for i in cities if i in myCities.keys()]
city = []
cityCode = []
for lst in myCodes[myCities[found[0]]]:
for k2, v2 in lst.iteritems():
city.append(k2)
cityCode.append(v2)
row[0] = ",".join(city)
row[1] = myCities[found[0]]+"_".join(cityCode)
rows.updateRow(row)
print "Done."
... View more
02-20-2018
11:04 PM
|
2
|
13
|
2647
|
|
POST
|
For my previous test I was working in an IDE outside ArcMap (version 10.5). I will do some experimenting with your last code sample later today.
... View more
02-20-2018
02:10 PM
|
2
|
7
|
2186
|
|
POST
|
I was experimenting with your script and was able to get a feature from AGOL with the global ID intact. Since this is a URL request, I was wondering if the "where" clause might be part of the problem. Normally the 1=1 would be encoded as 1%3D1 (%3D would be the equals sign). Edit-- I did another try using where=1=1 (without escaping the equals sign) and was able to get the global IDs. Have you examined the returned json before inserting it into your database to verify the global IDs?
... View more
02-19-2018
08:15 PM
|
1
|
2
|
4225
|
|
POST
|
If you want to use the update cursor as Xander Bakker is suggesting, you will need to add a test so you can skip fields with null values: with arcpy.da.UpdateCursor(Feature, ('D2clust200m')) as curs:
for row in curs:
if row[0] is not None:
row[0] = row[0].replace(FCNameCut, "test")
curs.updateRow(row)
... View more
02-19-2018
04:03 PM
|
2
|
4
|
3617
|
|
POST
|
For your field calculator line, do you get better results if you try: # No quotes (") around FCNameCut
arcpy.CalculateField_management (Feature, "D2clust200m", '!D2clust200m!.replace(FCNameCut, \"test\")',"PYTHON_9.3")
May also want to use "PYTHON_9.3".
... View more
02-18-2018
08:51 PM
|
1
|
16
|
3617
|
|
POST
|
Are you running your code in the Python window inside ArcMap? Or are you using an IDE outside ArcMap?
... View more
02-15-2018
09:37 AM
|
0
|
0
|
1327
|
|
POST
|
If I understand your second illustration, the following is an idea. Table1 would become a search cursor, and Table2 an insert cursor. import itertools
table1 = [ ['x', 'T1'], ['y', 'T2'], ['z', 'T3'], ['k', 'T4'],
['a', 'Be1'], ['b', 'Be2'], ['c', 'Be3'],
['f', 'J1'], ['r', 'J5'], ['o', 'J2'],
['l', 'N2'], ['m', 'N3'], ['n', 'N1'] ]
table2 = [] # new table
myData = {} # empty dictionary
for row in table1:
item = ["".join(x) for _, x in itertools.groupby(row[1], key=str.isdigit)]
if item[0] in myData:
myData[item[0]].append({row[0] : item[1]})
else:
myData[item[0]] = [{row[0] : item[1]}]
# print myData
for k1, v1 in myData.iteritems():
city = []
cityCode = []
for l in v1:
for k2, v2 in l.iteritems():
city.append(k2)
cityCode.append(v2)
table2.append([",".join(city), k1+",".join(cityCode)])
print table2
# [['a,b,c', 'Be1,2,3'], ['f,r,o', 'J1,5,2'], ['x,y,z,k', 'T1,2,3,4'], ['l,m,n', 'N2,3,1']]
... View more
02-14-2018
10:02 PM
|
3
|
16
|
3730
|
|
POST
|
By any chance are you trying to overwrite an existing PNG? Also, there is a similar tool to export to PDF (arcpy.mapping.ExportToPDF). If you try it, do you also get an error?
... View more
02-14-2018
02:47 PM
|
0
|
4
|
3345
|
|
POST
|
If you attempt to save the PNG to a location that you do not have permission, ArcMap will give an error like the one you received.
... View more
02-14-2018
12:45 PM
|
0
|
0
|
3345
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|