# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# visfromodel.py
# Created on: 2013-08-28 11:53:51.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: visfromodel <Observer_location> <Distance__value_or_field_> <Height_above_surface__meters_> <Elevation_Surface_URL> <wkid> <polygon>
# Description:
# Shows areas that are visible and not visible to observers by a point location and a distance.
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Load required toolboxes
arcpy.ImportToolbox("C:/agsresources/visibility/VisibilityUtilities.tbx")

# Set Geoprocessing environments
arcpy.env.mask = ""

# Script arguments
Observer_location = arcpy.GetParameterAsText(0)
if Observer_location == '#' or not Observer_location:
    Observer_location = "Database Connections\\defensesolution.sde\\defensesolution.sde.observer" # provide a default value if unspecified

Distance__value_or_field_ = arcpy.GetParameterAsText(1)
if Distance__value_or_field_ == '#' or not Distance__value_or_field_:
    Distance__value_or_field_ = "1000 Meters" # provide a default value if unspecified

Height_above_surface__meters_ = arcpy.GetParameterAsText(2)
if Height_above_surface__meters_ == '#' or not Height_above_surface__meters_:
    Height_above_surface__meters_ = "2" # provide a default value if unspecified

Elevation_Surface_URL = arcpy.GetParameterAsText(3)

wkid = arcpy.GetParameterAsText(4)
if wkid == '#' or not wkid:
    wkid = "3857" # provide a default value if unspecified

polygon = arcpy.GetParameterAsText(5)

# Local variables:
Buffer__2_ = Observer_location
Cell_Size = Buffer__2_
output_value = Cell_Size
ImageServer_Layer = output_value
Output_Viewshed = ImageServer_Layer
vshed = Output_Viewshed
vshed__2_ = vshed
Output_above_ground_level_raster = ImageServer_Layer
obs = Observer_location
Observer__2_ = obs
Output_Feature_Class__2_ = Observer__2_
Refractivity_coefficient = "0.13"
Use_earth_curvature_corrections = "true"
Input_Image_Service = "http://stlpatrick.esri.com:6080/arcgis/services/ElevationServices/afghanistan/ImageServer"
fc = wkid

# Process: Buffer
arcpy.Buffer_analysis(Observer_location, Buffer__2_, Distance__value_or_field_, "FULL", "ROUND", "NONE", "")

# Process: Calculate Cell Size
arcpy.CalculateValue_management("CalcCellSize(r\"%Buffer (2)%\")", "def CalcCellSize(ds):\\n   width = arcpy.Describe(ds).extent.width\\n   height = arcpy.Describe(ds).extent.height\\n   cellsize = str(max(float(max(width,height))/2000.0,30.0))\\n   #return cellsize + \" \" + cellsize\\n   return cellsize", "Double")

# Process: Calculate Value
arcpy.CalculateValue_management("%Cell Size%", "", "Cell Size")

# Process: Make Image Server Layer
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "30"
arcpy.MakeImageServerLayer_management(Input_Image_Service, ImageServer_Layer, Buffer__2_, "", "NORTH_WEST", "Name", "0", "", Cell_Size)
arcpy.env.cellSize = tempEnvironment0

# Process: Copy Features
arcpy.CopyFeatures_management(Observer_location, obs, "", "0", "0", "0")

# Process: Add Field (2)
arcpy.AddField_management(obs, "OFFSETA", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Observer__2_, "OFFSETA", "%Height above surface (meters)%", "PYTHON", "")

# Process: CreatePolygonMask
arcpy.gp.toolbox = "C:/agsresources/visibility/VisibilityUtilities.tbx";
# Warning: the toolbox C:/agsresources/visibility/VisibilityUtilities.tbx DOES NOT have an alias.
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.CreatePolygonMask(...) with arcpy.CreatePolygonMask_ALIAS(...)
arcpy.gp.CreatePolygonMask(polygon, wkid)

# Process: Viewshed
tempEnvironment0 = arcpy.env.extent
arcpy.env.extent = "in_memory\\Buffer"
tempEnvironment1 = arcpy.env.cellSize
arcpy.env.cellSize = "30"
tempEnvironment2 = arcpy.env.mask
arcpy.env.mask = "in_memory\\Buffer"
arcpy.gp.Viewshed_sa(ImageServer_Layer, Output_Feature_Class__2_, Output_Viewshed, "1", Use_earth_curvature_corrections, Refractivity_coefficient, Output_above_ground_level_raster)
arcpy.env.extent = tempEnvironment0
arcpy.env.cellSize = tempEnvironment1
arcpy.env.mask = tempEnvironment2

# Process: Raster to Polygon
arcpy.RasterToPolygon_conversion(Output_Viewshed, vshed, "SIMPLIFY", "VALUE")

# Process: Dissolve
arcpy.Dissolve_management(vshed, vshed__2_, "gridcode", "", "MULTI_PART", "DISSOLVE_LINES")

