0

I want to make a Python script that will calculate the distance of the line I drew and output it in a special form, but I can't figure out what I'm doing wrong.

import arcpy
import pythonaddins

class ToolClass2(object):
    """Implementation for probn_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDown(self, x, y, button, shift):
        pass
    def onMouseDownMap(self, x, y, button, shift):
        print(x,y)
        return(x,y)
    def onMouseUp(self, x, y, button, shift):
        pass
    def onMouseUpMap(self, x, y, button, shift):
        print(x,y)
        return(x,y)
    def onMouseMove(self, x, y, button, shift):
        pass
    def onMouseMoveMap(self, x, y, button, shift):
        pass
    def onDblClick(self):
        pass
    def onKeyDown(self, keycode, shift):
        pass
    def onKeyUp(self, keycode, shift):
        pass
    def deactivate(self):
        pass
    def onCircle(self, circle_geometry):
        pass
    def onLine(self, line_geometry):
        onMouseDownMap(x1,y1)
        onMouseUpMap(x2,y2)
        dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
        message = "Your lenght:" + str(dist)
        pythonaddins.MessageBox(message, "Ruler")
    def onRectangle(self, rectangle_geometry):
        pass
2
  • 1
    What is this "special form" you refer to? Currently your code is writing a text message to a message box. Also a minor note: you have spelt the word length wrong in your message string
    – Hornbydd
    Commented Nov 9, 2020 at 15:34
  • special form - this "pythonaddins.MessageBox(message, "Ruler")", and i bad speek english.
    – easyfreeze
    Commented Nov 9, 2020 at 15:37

1 Answer 1

1

Try replacing the following lines of code:

def onLine(self, line_geometry):
    #onMouseDownMap(x1,y1)
    #onMouseUpMap(x2,y2)
    #dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
    message = "Your length: " + str(line_geometry.length)
    pythonaddins.MessageBox(message, "Ruler",0)

This will return the length of the line which may have more than two vertices, your code would have returned the straight line distance between the end vertices, if it was working.

3
  • thank you, everything worked, but the line itself is not visible, you can somehow edit it to change the thickness and color for example, and what is the distance measured kilometers?
    – easyfreeze
    Commented Nov 10, 2020 at 5:10
  • I don't think python addins allow you to change the symbology of your sketch, you need to explore the helpfile, happy to be proven wrong by another! The units are in your map units and as you have tagged this question to be specifically about ArcGlobe, which I have never used, I can only assume they will be in decimal degrees, so your original approach of calculating the straight line distance would not have be sensible for units in lat/long.
    – Hornbydd
    Commented Nov 10, 2020 at 16:46
  • can I somehow edit the coordinate system in the code for the entire ArcGlobe?
    – easyfreeze
    Commented Nov 10, 2020 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.