Select to view content in your preferred language

Rotating the Earth in a data frame to reflect a user-selected country

436
2
12-29-2011 11:27 AM
SteveSchilling
New Contributor
I have two data frames, one is a location map.  The Spatial Reference for the location map data frame is World from Space.  The user selects a country from a toolbox script.  I calculate the center of the selected country from its extent and would like to use the X and Y of that center point as the Longitude of Center and Latitude of Center for the Spatial Reference, so the earth rotates and the selected country is in the center of the view.  The output from the program is:

==============================
DataFrame Found: Location_Map
Layers DataFrame was found: Location_Map
The layer found: WORLD_COUNTRIES
"NAME" = 'Guatemala'
Center X: -90.2294998169
Center Y: 15.7832899094
PROJCS['The_World_From_Space',GEOGCS['GCS_Sphere_ARC_INFO',DATUM['D_Sphere_ARC_INFO',SPHEROID['Sphere_ARC_INFO',6370997.0,0.0]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Orthographic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Longitude_Of_Center',-72.5333333334],PARAMETER['Latitude_Of_Center',42.5333333333],UNIT['Meter',1.0]];-7008096.68738543 -7008096.68738543 642628066.972445;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision
==================================
PROJCS['The_World_From_Space',GEOGCS['GCS_Sphere_ARC_INFO',DATUM['D_Sphere_ARC_INFO',SPHEROID['Sphere_ARC_INFO',6370997.0,0.0]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Orthographic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Longitude_Of_Center',-90.2294998169],PARAMETER['Latitude_Of_Center',15.7832899094],UNIT['Meter',1.0]];-7008096.68738543 -7008096.68738543 642628066.972445;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision
========================

The first Spatial Reference is the result of exportToString(), the second is the result fed into loadFromString().  Unfortunately, after running the program, the data frame Spatial Reference does not change.  Is this the wrong approach?  Any suggestions??  Thanks


import arcpy
arcpy.AddMessage("Begin Processing:")
country_selected = arcpy.sys.argv[1] #country name as String
arcpy.AddMessage(country_selected + "...")
arcpy.env.workspace = r"D:\temp"
mxd = arcpy.mapping.MapDocument("CURRENT")
arcpy.AddMessage("=============")
volc_list = []
SR_list = []

for df in arcpy.mapping.ListDataFrames(mxd):
    arcpy.AddMessage("DataFrame Found: " + df.name)
    
    if df.name == "Location_Map":
        mxd.activeView = df.name
        arcpy.AddMessage("Layers DataFrame was found: " + df.name)
        for lyr in arcpy.mapping.ListLayers(mxd,"",df):
            
            if lyr.name == "WORLD_COUNTRIES":
                arcpy.AddMessage("The layer found: " + lyr.name)
                expression = ""'"NAME"'" = '"+country_selected+"'"
                arcpy.AddMessage(expression)
                arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression)
                countryextent = lyr.getSelectedExtent()
                minX = countryextent.XMin
                minY = countryextent.YMin
                maxX = countryextent.XMax
                maxY = countryextent.YMax
                difX = (maxX - minX) / 2
                difY = (maxY - minY) / 2
                cenX = minX + difX
                cenY = minY + difY
                arcpy.AddMessage("Center X: " + str(cenX))
                arcpy.AddMessage("Center Y: " + str(cenY))
                spref = df.spatialReference.exportToString()
                arcpy.AddMessage(spref)
                SR_list = spref.split(",")
                SR_list[16] = str(cenX) + "]" # long of center
                SR_list[18] = str(cenY) + "]" # lat of center
                SR_outstring = ",".join(SR_list)
                arcpy.AddMessage("==================================")
                arcpy.AddMessage(SR_outstring)
                df.spatialReference.loadFromString(SR_outstring)
                arcpy.RefreshActiveView()
Tags (2)
0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
I think I was able to get this to work by modifying how the df.spatialReference was being set. See the modified code below.  4 new lines are commented as "NEW LINE".

import arcpy
arcpy.AddMessage("Begin Processing:")
country_selected = arcpy.sys.argv[1] #country name as String
arcpy.AddMessage(country_selected + "...")
arcpy.env.workspace = r"C:\temp"
mxd = arcpy.mapping.MapDocument("CURRENT")
arcpy.AddMessage("=============")
volc_list = []
SR_list = []

for df in arcpy.mapping.ListDataFrames(mxd):
    arcpy.AddMessage("DataFrame Found: " + df.name)
    
    if df.name == "Location_Map":
        mxd.activeView = df.name
        arcpy.AddMessage("Layers DataFrame was found: " + df.name)
        for lyr in arcpy.mapping.ListLayers(mxd,"",df):
            
            if lyr.name == "Provinces":
                arcpy.AddMessage("The layer found: " + lyr.name)
                expression = ""'"NAME"'" = '"+country_selected+"'"
                arcpy.AddMessage(expression)
                arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression)
                countryextent = lyr.getSelectedExtent()
                minX = countryextent.XMin
                minY = countryextent.YMin
                maxX = countryextent.XMax
                maxY = countryextent.YMax
                difX = (maxX - minX) / 2
                difY = (maxY - minY) / 2
                cenX = minX + difX
                cenY = minY + difY
                arcpy.AddMessage("Center X: " + str(cenX))
                arcpy.AddMessage("Center Y: " + str(cenY))
                #spref = df.spatialReference.exportToString()
                SR = df.spatialReference #NEW LINE
                spref = SR.exportToString() #NEW LINE
                arcpy.AddMessage(spref)
                SR_list = spref.split(",")
                SR_list[16] = str(cenX) + "]" # long of center
                SR_list[18] = str(cenY) + "]" # lat of center
                SR_outstring = ",".join(SR_list)
                arcpy.AddMessage("==================================")
                arcpy.AddMessage(SR_outstring)
                #df.spatialReference.loadFromString(SR_outstring)
                SR.loadFromString(SR_outstring) #NEW LINE
                df.spatialReference = SR  #NEW LINE
                arcpy.RefreshActiveView()
0 Kudos
SteveSchilling
New Contributor
Yes!  That rotates the earth to the desired position.  I did not know that you set the spatial reference to a variable first, then export to string. 

When I run it from layout view, it switches to data view for several seconds, then switches back to layout view with the earth rotated. 

Thank you!!
0 Kudos