|
POST
|
The code used in Label Expressions is slightly different than code used in the Field Calculator. You will need to make adjustments. For VB code: ''' VB Function used in Label Class Expression
Function FindLabel ( [Prefix], [NAME], [Sufix] )
FindLabel = [Prefix] & " " & [NAME] & " " & [Sufix]
End Function
Note: Drop first and last line of VB script for code block
For Expression, you will use name of function
'''
arcpy.CalculateField_management(in_table=myTable,
field=labelField,
expression="FindLabel",
expression_type="VB",
code_block='FindLabel = [Prefix] & " " & [NAME] & " " & [Sufix]\n') For Python code (in case you need to go that route): ''' Python Function used in Label Class Expression:
def FindLabel ( [Prefix], [NAME], [Sufix] ):
return [Prefix] + " " + [NAME] + " " + [Sufix]
Note: For expression, replace square brackets with exclamation marks in first row.
For def code, remove remove brackets from around field names
'''
arcpy.CalculateField_management(in_table=myTable,
field=labelField,
expression="FindLabel( !Prefix!,!NAME!, !Sufix! )",
expression_type="PYTHON_9.3",
code_block='def FindLabel ( Prefix, NAME, Sufix ):\n return Prefix + " " + NAME + " " + Sufix\n')
... View more
03-17-2017
11:16 PM
|
0
|
0
|
3731
|
|
DOC
|
I asked the same question in this thread. You should be able to populate the GNSS fields if they are properly set-up. That thread also contains some help links.
... View more
03-16-2017
01:14 PM
|
0
|
0
|
16653
|
|
POST
|
Here's my python code to query the REST API. In my case, I was querying for a survey date, so substitute it for "sysDate". In the query dictionary, the where clause should insure that the date field is not null, but it could be adjusted to search for a date after/before a specific date. Limit the output to 1 row with "resultRecordCount". For additional help, see: Query (Feature Service/Layer) import arcpy, urllib, urllib2, json, sys, time, datetime, collections
from datetime import datetime, timedelta
# URL, referrer and tokenurl may vary based on ArcGIS Online or your server setup
# Credentials and feature service information
username = "" # user name here
password = "" # password here
# Adjust URL as required
URL = "https://services.arcgis.com/<xxxx>/arcgis/rest/services/<layername>/FeatureServer/0/query"
referer = "http://www.arcgis.com/"
tokenurl = "https://www.arcgis.com/sharing/rest/generateToken"
# obtain a token
query_dict = { 'username': username, 'password': password, 'referer': referer }
query_string = urllib.urlencode(query_dict)
token = json.loads(urllib.urlopen(tokenurl + "?f=json", query_string).read())
if "token" not in token:
print(token['error'])
sys.exit(1)
# build query dictionary
query_dict = {
"where" : "SurveyDate IS NOT NULL",
"outFields" : "OBJECTID, Business, SurveyDate",
"orderByFields" : "SurveyDate ASC",
"returnGeometry" : "true",
"resultRecordCount" : "1",
"f": "json", "token": token['token'] }
# to select all fields use: "outFields" : "*",
# to select individual fields use comma delimited list: "outFields" : "OBJECTID, SurveyDate",
# a date certain: "where" : "SurveyDate >= DATE '2016-05-29 18:30:00'",
# if you do not want geometry: "returnGeometry" : "false",
# resultRecordCount only applies if supportsPagination is true
# results in json format
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
features = json.loads(jsonResponse.read(),
object_pairs_hook=collections.OrderedDict)[u'features']
# print json.dumps(features, indent=4, sort_keys=False) # formatted json
print 'ObjectID\tBusiness\tSurvey Date\tX-lon\tY-lat'
for feature in features:
# AGO uses GMT/UTC, you may wish to convert to local time
surveyTime = time.strftime('%c', time.localtime(feature['attributes']['SurveyDate']/1000))
print str(feature['attributes']['OBJECTID']) + '\t' + feature['attributes']['Business'] + \
'\t' + surveyTime,
# if you want geometry; AGO returns web mercator, reproject if necessary
# print "x: " + str(feature['geometry']['x'])+ "\ty: " + str(feature['geometry']['y'])
# WGS 1984 : (4326) Lat/Lon
# WGS 1984 Web Mercator (auxiliary sphere) : (102100) or (3857)
ptGeometry = arcpy.PointGeometry(arcpy.Point(feature['geometry']['x'],feature['geometry']['y']),
arcpy.SpatialReference(3857)).projectAs(arcpy.SpatialReference(4326))
print "\t" + str(ptGeometry.firstPoint.X) +"\t" + str(ptGeometry.firstPoint.Y)
print "\nCompleted"
... View more
03-16-2017
10:36 AM
|
1
|
1
|
1476
|
|
POST
|
I use ArcGIS Online. Our path looks like: https://services.arcgis.com/xxxx/ArcGIS/rest/admin/services/Locations/FeatureServer/0 with "rest" inserted and no dot before "FeatureServer". But I think you would receive a bad URL message if what you were using was incorrect.
... View more
03-15-2017
09:46 AM
|
0
|
2
|
9812
|
|
POST
|
Another option might be to assume there are 72 records added per day. Then select at random one of those records. You could then use a "SELECT ... IN ( ...)" query. Edit: I've updated this to insure that records are separated by at least 36 (approximately 1/2 day's worth). It may not insure that one record per day is selected, but the average record is 1 day apart and should have a difference of at least 12h. from random import randint
# assume 72 records added per day, aproximately 1 every 20 minutes
# 365 days * 72 records would total 26280 records
# record ID should be apart by at least 36
idList = []
randomNum = 0
for x in range(1, 26280, 72):
if randomNum > 36:
randomNum = 36 - (72 - randomNum)
else:
randomNum = 0
randomNum = randint(randomNum, 72)
idList.append(randomNum + x)
# print randomNum + x
print "SELECT * FROM table WHERE ObjectID IN " + str(tuple(idList))
... View more
03-13-2017
09:54 PM
|
0
|
0
|
1680
|
|
POST
|
Just a rough idea of how you might do it, but it doesn't check separation by 12h.... Edit: I've added some code that should give you the 12h difference. import time
from datetime import datetime
from random import randint
startDay = int(time.mktime(datetime.strptime('2010-01-01','%Y-%m-%d').timetuple()))
stopDay = int(time.mktime(datetime.strptime('2010-01-31','%Y-%m-%d').timetuple()))
randomTime = 0
for day in range(startDay, stopDay, 86400):
if randomTime > 43200:
randomTime = 43200 - (86400 - randomTime)
else:
randomTime = 0
randomTime = randint(randomTime, 86399)
dateString = datetime.fromtimestamp(day+randomTime).strftime('%Y-%m-%d %H:%M:%S')
if randomTime < 43200:
# morning
print "SELECT TOP 1 column FROM table WHERE dateTimeField > '" + dateString + "' ORDER BY dateTimeField ASC"
else:
#afternoon
print "SELECT TOP 1 column FROM table WHERE dateTimeField < '" + dateString + "' ORDER BY dateTimeField DESC"
... View more
03-13-2017
09:08 PM
|
1
|
0
|
1680
|
|
POST
|
You may also want to ask your question in Web AppBuilder for ArcGIS. I don't believe AppBuilder has a widget to calculate fields, although it is being suggested as an idea Calculate Field widget. You may need to use the REST API Calculate operation to accomplish this.
... View more
03-13-2017
10:16 AM
|
1
|
0
|
1492
|
|
POST
|
Sorry, I may have gotten off topic. It was just to clarify the difference between null, type None, and an empty string.
... View more
03-10-2017
11:49 AM
|
0
|
2
|
1967
|
|
POST
|
And other issues possible with str(None) which equals "None": field1 = None
myfield = str(field1)+""
print len(myfield), myfield
4 None
... View more
03-10-2017
10:57 AM
|
0
|
1
|
1967
|
|
POST
|
Back to Michael's question. Check for type None and an empty (zero-length) string. field1 = "" # "Field1"
field2 = "Field2"
field3 = "" # "Field3"
datefield = "2014-12-13"
if (field1 is not None):
# check for empty string (assuming field1 is a string type)
if not len(field1):
field1 = None
print "field1 set to none"
# perhaps a similar check for datefield, etc.
if ( (field1 is not None) and (datefield is not None) ):
print field1 + '\n' + datefield[-2:]
elif (datefield is not None):
print '\n'.join(filter(None,(field2, field3, datefield[-2:])))
else:
print '\n'.join(filter(None,(field2, field3)))
... View more
03-10-2017
10:24 AM
|
0
|
4
|
1967
|
|
POST
|
I received an error with this: field1 = None
myfield = field1+""
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
... View more
03-10-2017
09:58 AM
|
0
|
4
|
1967
|
|
POST
|
Perhaps a variation of this: field1 = None # "Field1"
field2 = "Field2"
field3 = None # "Field3"
datefield = None # "2014-12-13"
if ( (field1 != None) and (datefield != None) ):
print field1 + '\n' + datefield[-2:]
elif (datefield != None):
print '\n'.join(filter(None,(field2, field3, datefield[-2:])))
else:
print '\n'.join(filter(None,(field2, field3)))
... View more
03-09-2017
05:23 PM
|
0
|
9
|
3367
|
|
POST
|
For reading and writing shapefiles: https://github.com/GeospatialPython/pyshp
... View more
03-06-2017
10:50 PM
|
2
|
0
|
1702
|
|
POST
|
This code may help with the debugging process by printing the property values. for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("LABELCLASSES"):
print "\n\nLayer name: " + lyr.name
print " Show Layer: " + str(lyr.showLabels)
print " Data Set: " + lyr.datasetName
print "Data Source: " + lyr.dataSource
... View more
03-06-2017
12:31 PM
|
0
|
0
|
3254
|
| 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
|