Skip To Content

Языки скриптов и ArcGIS REST API

В этом разделе

ArcGIS REST API работает с любым языком написания скриптов, который может выполнить обращение к веб-сервису RESTful через HTTP и проанализировать отклики. К ним относятся Python, Java, JavaScript, PowerShell, C#, Ruby, Scala, Perl и др.

В примерах REST API, приведенных в Справке ArcGIS Server, используется язык Python. При этом приведенные ниже примеры показывают, как вы можете написать аналогичный скрипт при помощи различных языков. Каждый скрипт запускает сервис геометрии, установленный с ArcGIS Server.

Python

Данный скрипт запускает сервис геометрии при помощи языка Python.

Для запуска сервиса требуются права администратора, поэтому при запуске данного скрипта потребуется указать имя пользователя и пароль учетной записи с административными правами.

# Demonstrates how to start a service by starting the geometry service
# This service is included with every ArcGIS Server and is stopped by default

# For Http calls
import httplib, urllib, json

# For system tools
import sys

# For reading passwords without echoing
import getpass


# Defines the entry point into the script
def main(argv=None):
    # Print some info
    print
    print "This tool is a sample script that starts the Geometry service on a Server."
    print
    
    # Ask for admin/publisher user name and password
    username = raw_input("Enter user name: ")
    password = getpass.getpass("Enter password: ")

    # Ask for server name
    serverName = raw_input("Enter server name: ")
    serverPort = 6080
    
    # Get a token
    token = getToken(username, password, serverName, serverPort)
    if token == "":
        print "Could not generate a token with the username and password provided."
        return
    
    # Construct URL to start a service - as an example the Geometry service
    serviceStartURL = "/arcgis/admin/services/utilities/Geometry.GeometryServer/start"
    
    # This request only needs the token and the response formatting parameter 
    params = urllib.urlencode({'token': token, 'f': 'json'})
    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    
    # Connect to URL and post parameters    
    httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", serviceStartURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while attempting to start the service."
        return
    else:
        data = response.read()
        httpConn.close()
        
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):          
            print "Error returned by operation. " + data
        else:
            print "Operation completed successfully!"
        
        return


# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort):
    # Token URL is typically http://server[:port]/arcgis/admin/generateToken
    tokenURL = "/arcgis/admin/generateToken"
    
    # URL-encode the token parameters
    params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    
    # Connect to URL and post parameters
    httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", tokenURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while fetching tokens from admin URL. Please check the URL and try again."
        return
    else:
        data = response.read()
        httpConn.close()
        
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):            
            return
        
        # Extract the token from it
        token = json.loads(data)        
        return token["token"]            
        

# A function that checks that the input JSON object 
#  is not an error object.  
def assertJsonSuccess(data):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True
    
        
# Script start
if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Windows PowerShell

Данный скрипт запускает сервис геометрии при помощи языка Windows PowerShell. Он представляет собой пример написания других скриптов PowerShell при помощи ArcGIS REST API.

# Demonstrates how to start a service by starting the geometry service.# This service is included with every ArcGIS Server 

and is stopped by default.

# To run this script, you must set the the appropriate security settings.
#  For more information about Powershell security, visit the following URL: 
#  http://www.windowsecurity.com/articles/PowerShell-Security.html


# provides serialization and deserialization functionality for JSON object
Add-Type -AssemblyName System.Web.Extensions


# Defines the entry point into the script
function main(){
  # Print some info
  echo "This is a sample script that starts the ArcGIS Server geometry service."
  
  # Ask for admin/publisher user name
  $username = read-host "Enter user name"
  $securePassword = read-host -AsSecureString "Enter password"
  
  # Ask for Admin/publisher password
  # Use Marshal Class to decode the secure string. Reference:
  #  http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx
  $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($securePassword)
  $password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
  [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
  
  # Ask for servername
  $serverName = read-host "Enter server name"
  $serverPort = 6080
  
  # Get a token
  $token = token $username $password $serverName $serverPort
  if(!$token){
    write-warning "Could not generate a token with the username and password provided"
    
    return
  }
  
  # construct URL to start a service - as an example the Geometry service
  $url = "http://${serverName}:${serverPort}/arcgis/admin/services/Utilities/Geometry.GeometryServer/start"
  $parameters = "token=${token}&f=json"
  
  # Construct a HTTP request
  $http_request = New-Object -ComObject Msxml2.XMLHTTP
  $http_request.open('POST', $url, $false)
  $http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  
  # Connect to URL and post parameters
  try{
    $http_request.send($parameters)
  }
  catch{
    write-warning 'There were exceptions when sending the request'
    
    return
  }
  
  # Read response
  if($http_request.status -eq 200){
    # Check that data returned is not an error object
    if(assertJsonSuccess $http_request.responseText){
      echo "Operation completed successfully!"
    }
    else{
      write-warning "Error returned by operation."
      write-warning $http_request
    }
  }
  else{
    write-warning "Error while attempting to start the service."
  }
}


# A function to generate a token given username, password and the adminURL.
function token([string]$username, [string]$password, [string]$serverName, [int]$serverPort){
  # Token URL is typically http://server[:port]/arcgis/admin/generateToken
  $url = "http://${serverName}:${serverPort}/arcgis/admin/generateToken"
  $parameters = "username=${username}&password=${password}&client=requestip&f=json"
  
  # Construct a HTTP request
  $http_request = New-Object -ComObject Msxml2.XMLHTTP
  $http_request.open('POST', $url, $false)
  $http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  
  # Connect to URL and post parameters
  $token = ''
  try{
    $http_request.send($parameters)
  }
  catch{
    write-warning 'There were exceptions when sending the request'
    
    return $token
  }
  
  # Read response
  if($http_request.status -eq 200){
    # Check that data returned is not an error object
    if(assertJsonSuccess $http_request.responseText){
      # Extract the token from it
      $jsonSerializer = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
      $jsonObj = $jsonSerializer.DeserializeObject($http_request.responseText)
      
      $token = $jsonObj['token']
    }
  }
  else{
    write-warning "Error encountered when retrieving an administrative token. Please check your credentials and try again."
  }
  
  $token
}


# A function that checks that the input JSON object 
#  is not an error object.  
function assertJsonSuccess([string]$data){
  $jsonSerializer = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
  $jsonObj = $jsonSerializer.DeserializeObject($data)
  
  if($jsonObj['status'] -eq 'error'){
    write-warning "JSON object returns an error. Data => ${data}"
    $FALSE
  }
  else{
    $TRUE
  }
}


# Script start
main

Perl

Данный пример скрипта запускает сервис геометрии при помощи языка Perl. Скрипт выполняет постоянные попытки выполнить безопасный вход (шифрование имени пользователя и пароля). Если HTTPS не настроен для ArcGIS Server, то попытка безопасного входа будет неудачной и скрипт, по умолчанию, попытается выполнить небезопасный вход. Если вы не желаете выполнять попытку небезопасного входа, используйте параметр -e для необходимых безопасных входов.

В данном скрипте используются библиотеки LWP и HTTP, которые обычно включены во все версии Perl. Данный скрипт не требует наличия программного обеспечения Esri, установленного в системе, с которой выполняется его запуск.

При изменении данного скрипта для вашего собственного использования, необходимо учитывать, что сервис геометрии находится в папке Utilities, и URL, используемый в скрипте, потребуется изменить для соответствия другим именам папок.

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET POST);

my $ua = LWP::UserAgent->new;
$ua->timeout(300); 	#services start within 5 minutes or timeout

my ($user, $password, $serverName, $port, $requireEncryption) = getUserInput();
my $baseUrl ="http://$serverName:$port/arcgis";

my $token = getToken($baseUrl, $user, $password, $requireEncryption);
startService($baseUrl, $token, "Geometry", "GeometryServer");


sub getToken {
	# Attempt to login securely.  
	# If it fails and encryption required, tell user.
	# If it fails and encryption not required, try insecure login.
	
	my ($baseUrl, $user, $password, $requireEncryption) = @_;
	my $secureUrl = $baseUrl;
	$secureUrl =~ s/http/https/;
	$secureUrl =~ s/:6080/:6443/;

	my $token = makeTokenRequest($secureUrl, $user, $password);
	return $token if (defined $token);

	if ($requireEncryption) {
		print "Unable to login. Either the server doesn't support https, or the server name,\nusername, or password might be incorrect.\n\n";
		exit(1);
	}

	$token = makeTokenRequest($baseUrl, $user, $password);
	return $token if (defined $token);
	
	print "Unable to get token.  The server name, username, or password might be incorrect.\n";
	exit(1);
}


sub makeTokenRequest {
	my ($url, $user, $password) = @_;
	my $response = makeHttpPost("$url/admin/generateToken", {username=>$user, password=>$password, client=>"requestip", expiration=>30, f=>'json'});
	if ($response =~/\{\"token\"\:\"(.*)\",/) {
		return $1;
	} else {
		return undef;
	}
}

sub startService {
	my($baseUrl, $token, $serviceName, $serviceType) = @_;

	my $response = makeHttpPost("$baseUrl/admin/services/Utilities/$serviceName.$serviceType/start", {f=>'json', token=>$token});
	if ($response eq "") {
		print "ERROR: Unable to start the service.  Unable to make http connection to server.\n";
	} elsif ($response =~/success/) {
		print "Service $serviceName.$serviceType successfully started.\n";
	} elsif ($response =~ /"code":404/) {
		print "Service $serviceName.$serviceType was not found. Please check for typos.\n";
	}
}




sub makeHttpPost {

        my $url         = $_[0];
        my %parameter   = %{$_[1]};

        my $req = HTTP::Request->new(POST => $url);
        $req->content_type('application/x-www-form-urlencoded');
        my $requestParameters = "";
        foreach my $key ( keys %parameter) {
                $requestParameters .= "$key=$parameter{$key}&"
        }

        $req->content($requestParameters);
        my $res = $ua->request($req);
        if ($res->is_success) {
                return $res->content;
        } else {
	        return "";
	}
}

sub getUserInput {

	my ($user, $password, $serverName, $port, $requireEncryption);
	$serverName = "localhost";
	$port = "6080";
	$requireEncryption = 0;

	if ($#ARGV < 0) {
		print "This tool is a sample script that starts the Geometry service on a Server.\n\n";
		print "\t-u\tArcGIS Server publisher or admin user. Required\n";
		print "\t-p\tPassword for the user. Required.\n";
		print "\t-s\tServer machine. Optional, default is localhost.\n";
		print "\t-t\tPort.  Optional, default is 6080.\n";
		print "\t-e\tRequire secure login.\n";
		print "\n\n\tExample: startGeometryService -u admin -p password.\n";
		print "\tExample: startGeometryService -u admin -p password -s gis1.\n";
		print "\tExample: startGeometryService -u admin -p password -e.\n";
		print "\tExample: startGeometryService -u admin -p password -s gis1 -t 80.\n\n";
		print "Warning: This script always tries to login securely through https (encrypted).\n";
		print "If that fails, it tries an insecure login since ArcGIS Server may not be\n";
		print "configured for https.  Use the -e option to avoid ever using an insecure login.\n\n\n";
		exit();
	} else {
		for my $i (0 .. $#ARGV) {
			$user = $ARGV[$i+1] if ($ARGV[$i] eq "-u");
			$password = $ARGV[$i+1] if ($ARGV[$i] eq "-p");
			$serverName = $ARGV[$i+1] if ($ARGV[$i] eq "-s");
			$port = $ARGV[$i+1] if ($ARGV[$i] eq "-t");
			$requireEncryption = 1 if ($ARGV[$i] eq "-e");
		}
	}

	if (! defined $user || ! defined $password) {
		print "A user was not provided using the correct syntax.\n" if (! defined $user);
		print "A password was not provided using the correct syntax.\n" if (! defined $password);
		exit(1);
	}

	return ($user, $password, $serverName, $port, $requireEncryption);
}

Scala

Следующий пример написан на языке Scala. Он позволяет вам запустить или остановить сервис геометрии.

// Sample usage of the ArcGIS for Server Administrator API
// - Dispatch (http://dispatch.databinder.net)
// - LiftJSON
// ...demonstrates how to START or STOP the Geometry Service in an ArcGIS Server site

import dispatch._
import dispatch.liftjson._
import Http._
import dispatch.liftjson.Js._
import net.liftweb.json.JsonAST._

object StartStopGeometryService {

	def main(args: Array[String]){

                
		try{
		    print("Enter Command (START or STOP): ")       
            val command = Console.readLine()
            print("Enter host name: ") 
            val host = Console.readLine() 
            print("Enter port:") 
		    val port = Console.readLine()
		    print("Enter username: ") 
		    val username = Console.readLine()
		    print("Enter password (WARNING! Password will show up as clear text): ") 
		    val password = Console.readLine()
		    
		   // if (!command.equalsIgnoreCase("start") || !command.equalsIgnoreCase("stop")){
		   //   println("Usage: StartGeometryService <START | STOP> <hostname> <port> <username> <password>")
		   //   sys.exit()
		   // }
		      

		    // Http Request
            val http = new Http

		
		    // Request a TOKEN...
		    val u = url("http://" + host + ":" + port + "/arcgis/admin/generateToken") << Map("f" -> "pjson","username" -> username.toString, "password" -> password.toString,"client" -> "requestip" , "expiration" -> "1000")
		    val token = http(u ># {json => (json \\ "token").values})


		    // Use the token to make a call to the Geometry Service to START or STOP (STOP, in this case)
		    val geomStartURL = url("http://" + host + ":" + port + 			"/arcgis/admin/services/Utilities/Geometry.GeometryServer/" + command) << Map("f" -> "pjson", "token" -> token.toString)
		    val response = http(geomStartURL ># {json => json})
	
		    println(response)

		}catch{
             case e: Exception => println(e.getMessage)
        }
		
	}

}