2

I'm about to create a script wich creates points from lines on a specified layer with a length and a rotation field.

Geometry:

  • The point goes to the first vertex of the line

Attributes:

  • length filed: distance between the first and second vertex
  • rotate field: clockwise rotation angle from north

Here is the code: http://paste.ubuntu.com/8360617/

Something is wrong because sometimes it works and sometimes not. I can't find the error. Please, can you check the code? What am I doing wrong?

Addition 1: With my test layers with 4-5 lines works fine, points are created with fields, but on working layers (1000+ lines) not working. No points created, no messages, no logs. Sometimes QGIS crashes and quits without any message. QGIS: 2.4.0 OS: Ubuntu 14.04 LTS 64 bit, Windows 7 64 bit

Addition 2: I've made these changes. Works well. Some cases i see this message:

[Errno 9] Bad file descriptor See log for more details

at the log i see this error message:

Uncaught error while executing algorithm Traceback (most recent call last): Traceback (most recent call last): File "C:/PROGRA~1/QGISCH~1/apps/qgis/./python/plugins\processing\core\GeoAlgorithm.py", line 217, in execute self.processAlgorithm(progress) File "C:/PROGRA~1/QGISCH~1/apps/qgis/./python/plugins\processing\script\ScriptAlgorithm.py", line 265, in processAlgorithm exec script in ns File "", line 68, in IOError: [Errno 9] Bad file descriptor

the current code is availabe here: http://paste.ubuntu.com/8364483/

Sample data: https://dl.dropboxusercontent.com/u/143148/temp/test.zip

  • lines: LEAG_VIZ_V
  • points: LEAG_VIZ
    • length field: HOSSZ
    • rotate field: GMrotation
  • Welcome to GIS.SE falu, can you expand a bit more on 'not working' please? Are you getting errors (please include error messages) or running without errors but not getting the expected results? – Michael Stimson Sep 16 '14 at 21:46
  • With my test layers with 4-5 lines works fine, points are created with fields, but on working layers (1000+ lines) not working. No points created, no messages, no logs. – falu Sep 17 '14 at 5:48
  • @falu, Note, you don't need the if dy == 0 check in the code. atan2 handles that scenario for you. – WhiteboxDev Sep 17 '14 at 12:06
  • @WhiteboxDev thank you! But i think this is not the real problem – falu Sep 17 '14 at 13:40
3

There appears to be a problem with your calculation of the rotation angle. You should replace the following code:

if dy == 0:
      #avoid divide by zero
      dy = 0.0000000001

    protate = abs( math.atan(dx/dy) ) * 180 / math.pi

    if (dx >= 0) and (dy >= 0):
      # 1st quarter: dx>0, dy>0
      protate = protate
    elif (dx >= 0) and (dy < 0): 
      # 2nd quarter: dx>0, dy<0
      protate = 180 - protate
    elif (dx < 0) and (dy < 0): 
      # 3rd quarter: dx<0, dy<0
      protate = 180 + protate
    elif (dx < 0) and (dy >= 0): 
      # 4th quarter: dx<0, dy>0
      protate = 360 - protate

    print protate

with this:

protate = 90 - math.degrees(math.atan2(dy, dx))
if (protate < 0):
   protate += 360

Not only is the code much shorter, it calculates your angles correctly. Consider the case where dx = 74 and dy = 91. Your method computes an angle of zero, whereas the code above computes it correctly as 39.1 degrees.

As an aside, if you have many points and performance becomes an issue, you may want to trade out this line:

plength = math.sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) )

for:

plength = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))

Power functions are computationally expensive. I hope that helps out.

  • Its working, but not solves the problem. See the addtional informations on top. Thx. – falu Sep 17 '14 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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