|
POST
|
That was trick! Thank you for your help, I'm such a noob at this.
... View more
05-02-2019
03:55 PM
|
0
|
3
|
3963
|
|
POST
|
I read that wiki, not sure how to do this: 'Access your add-in project's Properties page through its context menu.'
... View more
05-02-2019
03:46 PM
|
0
|
0
|
3963
|
|
POST
|
The config was auto generated, and I have not touched it since adding the button to the project. Looks like it added _Module, _Group1, _Button1 to the namespace name. Do these all need to be identical?
... View more
05-02-2019
03:39 PM
|
0
|
6
|
3963
|
|
POST
|
Hi Uma, thanks for the reply! So when I try just that line and click the button in Pro it grays out and nothing happens. I tried a variety of the suggestions on the stackoverflow site and it's the same behavior, button grays out on first click and no browser window opens. The final suggestion seemed promising but did not work. What is commented out is what I tried just before that. namespace StreetViewButton
{
internal class OpenGoogle : Button
{
protected override void OnClick()
{
var processes = Process.GetProcessesByName("Chrome");
var path = processes.FirstOrDefault()?.MainModule?.FileName;
string url = "http://google.com";
//System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
//myProcess.StartInfo.UseShellExecute = true;
//myProcess.StartInfo.FileName = url;
Process.Start(path, url);
}
}
}
... View more
05-02-2019
03:27 PM
|
0
|
8
|
3963
|
|
POST
|
I'm still bitter with ESRI for not bringing Python Add-Ins over to Pro.. I have never coded in C#/.NET and know very little about ArcObjects. My Python skills I consider to be strong, not just scripting but app development, so I think I can pick up on the .NET SDK, though I am reluctant to get into this rabbit hole. I have VS and the SDK up and running, went through some tutorials, just looking for some guidance on a specific solution here. A while back I created a little ArcMap Add-In, just a single toolbar button that will open Google Street View in your default browser when the user clicks a location on the map. I'd like to deploy a button like this in Pro, but after searching and searching for 'C# open a web page on click event' all I was coming up with was ASP.NET stuff and I have zero knowledge on any of this stuff lol! I was basically looking for a one liner to add into the onClick event of my button.cs code to just open Google.com to start with. Below is the Python Add-In code that accomplishes this, and I am not looking for anyone to code this entirely for me, but maybe some suggestions, guidance, resources, etc. All this does is grab the coordinates from where the user clicks and feeds that into a url to open street view. import arcpy
import pythonaddins
import webbrowser
import threading
class OpenStreetView(object):
"""Implementation for StreetView_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 3 # cross hair cursor
def onMouseDownMap(self, x, y, button, shift):
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
sr_df = df.spatialReference
pt = arcpy.Point(x,y)
pt_geom = arcpy.PointGeometry(pt, sr_df)
new_pt_geom = pt_geom.projectAs("WGS 1984")
new_pt = new_pt_geom.firstPoint
new_X = new_pt.X
new_Y = new_pt.Y
## message = "Projected: " + str(x) + ", " + str(y) + " Geographic: " + str(new_X) + ", " + str(new_Y)
## pythonaddins.MessageBox(message, "Coordinates")
maps_url = "https://maps.google.com/maps?q=&layer=c&cbll=" + str(new_Y) + "," + str(new_X)
threading.Thread(target=webbrowser.open, args=(maps_url,0)).start()
... View more
05-01-2019
10:58 AM
|
0
|
13
|
6031
|
|
POST
|
I just ran into a situation where I needed to figure out field mapping with Python, and my situation is slightly different than the examples posted here so I thought I'd share my solution. The Problem: We have a web map that has what we call 'compiled layers'. These are layers built from features joined to SQL Server views, which can be incredibly slow and impractical on a web map. I built a python module to reuse in scripts that run every night and perform the join, delete and append to target feature classes in our database. Originally these were set up with models, but now we are going to create a lot more and that is the reason behind scripting this. Whenever these models were first created, the target feature class only contained a subset of fields from the joined tables - so field mapping came into play. The examples above assume that you know what fields you are mapping, or that you already have a field map string. The method I came up with is generic enough to create a field map with no knowledge of the append fields or target fields. And since we are an organization, some trickery is involved to match the fields created after the join since they aren't just a field name but are something like <DB name>.DBO.<feature name>. For info on what the EmailService module is and how to get email notifications when scripts fail, check this out. #-------------------------------------------------------------------------------
# Name: CompiledLayers.py
#
# Author: Dylan Harwell - GIS Tech - City of GJ
#
# Created: 25/04/2019
# Copyright: (c) dylanh 2019
#
#-------------------------------------------------------------------------------
import arcpy
arcpy.env.overwriteOutput = True
class CompiledLayers(object):
"""Custom class to create compiled layers in lieu of joined layers on
web maps."""
def __init__(self):
self.ignore = ['OBJECTID', 'Shape', 'Shape.STArea()',
'Shape.STLength()', 'STArea()', 'STLength()',
'GlobalID', 'ESRI_OID', 'SHAPE' ]
def createCompiledLayer(self, join_fc, in_field, join_table,
join_field, join_type, target_fc):
"""Makes feature layer, joins to sql server table, sets up fieldmapping,
deletes and appends target feature class in gjgisweb.sde."""
arcpy.MakeFeatureLayer_management(join_fc, 'temp_lyr')
arcpy.AddJoin_management('temp_lyr', in_field, join_table, join_field,
join_type)
target_fields = [f for f in arcpy.ListFields(target_fc)
if f.name not in self.ignore]
append_fields = [f for f in arcpy.ListFields('temp_lyr')
if f.name.split('.')[-1] not in self.ignore]
fields_to_map = []
for a in append_fields:
for t in target_fields:
if a.name.split('.')[-1].lower() == t.name.lower():
fields_to_map.append((a.name, t.name))
fieldmapping = arcpy.FieldMappings()
for fields in fields_to_map:
field_map = arcpy.FieldMap()
field_map.addInputField('temp_lyr', fields[0])
field_map.addInputField(target_fc, fields[1])
fieldmapping.addFieldMap(field_map)
arcpy.DeleteFeatures_management(target_fc)
arcpy.Append_management('temp_lyr', target_fc, 'NO_TEST', fieldmapping)
def main():
pass
if __name__ == '__main__':
main()
################### Example of using this module in a script ########################
import CompiledLayers as CL
import EmailService as ES
def getTime():
"""Function to fetch current date and time."""
date_time = time.strftime("%Y%m%d") + "_" + time.strftime("%H%M%S")
return date_time
# Set path to log file
log = 'T:...\\Log\\CompileNorthstarTrashParcels' + getTime() + '.txt'
# Set variables
join_fc = 'T:...\\dbConnections\\gjgisprod.sde\\gjgisprod.DBO.Parcel'
in_field = 'PARCEL_NUM'
join_table = 'T:...\\dbConnections\\ns.sde\\Northstar64_Live.dbo.gj_trashdata'
join_field = 'PARCEL'
join_type = 'KEEP_COMMON'
target_fc = 'T:...\\dbConnections\\gjgisweb.sde\\gjgisweb.DBO.CompiledLayers\\gjgisweb.DBO.northstarTrashParcels'
with open(log, 'w') as log_file:
try:
log_file.write('Calling CompiledLayers module at ' + getTime() + '\n')
compiler = CL.CompiledLayers()
compiler.createCompiledLayer(join_fc, in_field, join_table, join_field,
join_type, target_fc)
log_file.write('Succeeded creating compiled layer at ' + getTime())
except Exception as e:
log_file.write('Failed at ' + getTime() + '\n')
log_file.write(e.message)
# Send email to server admin if script fails
service = ES.EmailService()
subject = 'Server Script Failure!'
body = 'CompiledNorthstarTrashParcels.py failed'
recip = 'jacksont@gjcity.org'
service.configMessage(subject, body)
service.sendEmail(recip)
... View more
04-30-2019
01:45 PM
|
0
|
0
|
1470
|
|
POST
|
I too discovered that, and unfortunately, no the aliases have been stored in this specific Pro project, not with the data source. I have a clever yet cumbersome workaround, but hey, everything I had to do in the original script to turn the fields off was a clever and cumbersome workaround, what's another. Save the layer file, hack into the JSON and get the field aliases like this: **shhh don't tell esri we are smart enough to do this** aliases = []
with open(lyrx_path) as json_file:
data = json.load(json_file)
field_info = data['layerDefinitions'][0]['featureTable']['fieldDescriptions']
for f in field_info:
aliases.append(f['alias'])
... View more
04-17-2019
01:02 PM
|
4
|
0
|
4031
|
|
POST
|
This is a follow up problem to my previous dilemma: Turn Off Fields with Python which is working, however, we keep discovering things that need to be added to preserve all the properties of the original project (TOC order and visibility, group layers, disable popups, etc.). The final thing (we think) that needs to be preserved is field aliases. Now I know there are at least two ways to access this property - arcpy.ListFields() or arcpy.Describe().fields Neither of which seem to work - they always list the actual field names, not the aliases, even when aliases exist. I have been testing this directly in the Python window within Pro. Below is a screenshot of a test layer field table, with the two methods to access field properties below. Is this another bug in Pro or is there something here that I completely missing..??
... View more
04-17-2019
10:12 AM
|
2
|
3
|
4618
|
|
POST
|
Gotta love those extremely insightful error messages
... View more
04-16-2019
07:24 AM
|
0
|
0
|
1021
|
|
POST
|
Have you tried the old 'unplug and plug back in' technique?
... View more
04-15-2019
01:37 PM
|
0
|
2
|
1021
|
|
POST
|
Same thoughts. Each click, even just toggling layers on and off in the TOC, causes everything to gray out for several seconds. Also this Basic Geoprocessing in Pro is SLOW
... View more
04-15-2019
12:52 PM
|
15
|
0
|
27075
|
|
POST
|
Yea that is odd. I was not generating lines when I ran this analysis, only polygons. I was doing 4 and 8 minute drive times, and I noticed the 4 minute polygons would turn out pretty detailed and follow roads around large open space areas. But the 8 minute polygons were much more generalized in the middle and all the open space areas with no roads were filled in.
... View more
04-12-2019
12:42 PM
|
0
|
0
|
2973
|
|
POST
|
I decided to just do it all in Pro from now on to stay consistent, and I think I remember liking the way the Pro polys looked more lol
... View more
04-12-2019
12:28 PM
|
0
|
5
|
2973
|
|
POST
|
After lots and lots of testing, I imagine this is a bug and will be opening a case with tech support. If this was built into the design of arcpy, there is no reason I can think of that the FieldInfo properties should not be honored on data coming from a replica database since they are properties of the layer in the map/project and not properties of the data in the database. I did, however, manage to figure out a workaround, cobbled together with someone else's workaround for preserving the symbology, and my own hack of the JSON code in the lyrx file to preserve other properties of our maps. The solution is rather ridiculous and overly complex for what should be such a simple task of turning off fields. I hope ESRI and the Python community can see the humor in this code # Import modules and set workspace
import arcpy, json, os
workspace = "C:\\...\\WebLayers_COPY"
arcpy.env.workspace = workspace
arcpy.env.overwriteOutput = True
# Set up folder paths and fields to turn off
layer_folder = "C:\\...\\TempLayers"
proj_path = "C:\\...\\webLayersArcGISAPP_COPY2.aprx"
fields_to_turn_off = ['created_user', 'created_date',
'last_edited_user', 'last_edited_date']
# Create project object
project = arcpy.mp.ArcGISProject(proj_path)
# List maps in project and layers in maps
maps = project.listMaps()
# Initiate loop through maps and layers
for map in maps:
layers = map.listLayers()
for lyr in layers:
# Pass over group layers
if lyr.isGroupLayer:
pass
# Pass over any data not coming from gjgisprod.sde
elif 'gjgisprod' not in lyr.dataSource:
pass
# Pass over any data using Parcels
elif 'Parcel' in lyr.dataSource:
pass
# Begin absurd process
else:
# Get field info, loop through and turn off desired fields
field_info = arcpy.Describe(lyr).fieldInfo
for i in range(0, field_info.count):
if field_info.getFieldName(i) in fields_to_turn_off:
field_info.setVisible(i, "HIDDEN")
# Make feature layer, save to lyr file, make feature layer with new
# field info, apply symbology, save to lyr file again
# --dumb workaround: fieldinfo not honored on replica data
# --must make feature layer and save as lyr twice for it to work
# --dumb symbology/naming workarounds
name = lyr.name
out_lyr_path = os.path.join(layer_folder, (name + "_1.lyrx"))
arcpy.MakeFeatureLayer_management(lyr, "temp_lyr", "", "", field_info)
arcpy.SaveToLayerFile_management("temp_lyr", out_lyr_path)
arcpy.MakeFeatureLayer_management(out_lyr_path, "temp_lyr2",
"", "", field_info)
arcpy.ApplySymbologyFromLayer_management("temp_lyr2", lyr, "", "MAINTAIN")
new_lyr_path = os.path.join(layer_folder, (name + ".lyrx"))
arcpy.SaveToLayerFile_management("temp_lyr2", new_lyr_path)
arcpy.Delete_management(out_lyr_path)
new_lyr_file = arcpy.mp.LayerFile(new_lyr_path)
new_lyr = new_lyr_file.listLayers()[0]
new_lyr.updateConnectionProperties(new_lyr.connectionProperties,
lyr.connectionProperties)
# Check leyer properties and preserve
if not lyr.visible:
new_lyr.visible = False
if lyr.transparency:
new_lyr.transparency = lyr.transparency
if lyr.minThreshold:
new_lyr.minThreshold = lyr.minThreshold
if lyr.maxThreshold:
new_lyr.maxThreshold = lyr.maxThreshold
new_lyr.name = name
new_lyr_file.save()
# Hack into lyrx JSON code and collapse layer and disable popups
with open(new_lyr_path) as json_lyr:
data = json.load(json_lyr)
data['layerDefinitions'][0]['expanded'] = False
data['layerDefinitions'][0]['showPopups'] = False
with open(new_lyr_path, "w") as json_lyr:
json.dump(data, json_lyr, indent=4)
# Must delete lyr and create another for the JSON changes
del new_lyr_file
new_new_lyr = arcpy.mp.LayerFile(new_lyr_path)
# Finally, switch out layers in map
map.insertLayer(lyr, new_new_lyr, "AFTER")
map.removeLayer(lyr)
# Save project and clean up (still does not remove locks for some reason..)
project.save()
del project
del workspace
... View more
04-12-2019
11:33 AM
|
6
|
2
|
11227
|
|
POST
|
I've compared both and the polygons still were slightly different. Not as radically different as in this example though.
... View more
04-12-2019
10:35 AM
|
0
|
0
|
1179
|
| Title | Kudos | Posted |
|---|---|---|
| 2 | 06-24-2019 01:27 PM | |
| 4 | 04-17-2019 01:02 PM | |
| 1 | 04-12-2019 07:47 AM | |
| 1 | 04-02-2019 12:51 PM | |
| 15 | 04-15-2019 12:52 PM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|