Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.51 KB | None | 0 0
  1. from pywps.Process.Process import WPSProcess
  2. from osgeo import ogr
  3. from decimal import Decimal
  4. import time
  5. import math
  6. class Process(WPSProcess):
  7.     def __init__(self):
  8.         WPSProcess.__init__(self,
  9.                             identifier = "poll_tin_reducer", # must be same, as filename
  10.                             title="Tin-Reducer",
  11.                             version = "0.1",
  12.                             storeSupported = "true",
  13.                             statusSupported = "true",
  14.                             abstract="reduces the points of a given triangulated irregular network by a variable factor",
  15.                             grassLocation =False)
  16.         self.Output1=self.addLiteralOutput(identifier="output1", title="reduced TIN points as WKT or GMl")
  17.         self.Output2=self.addLiteralOutput(identifier="output2", title="computation time (to compare different approaches)")
  18.         self.Input1 = self.addLiteralInput(identifier="input1", title = "path of the shapefile to be processed", type = type(""))
  19.         self.Input2 = self.addLiteralInput(identifier="input2", title = "minimal distance between points")
  20.  
  21.     def execute(self):
  22.  
  23.         #initialize clock
  24.         startTime = time.clock()
  25.         global lowDists
  26.         lowDists = []
  27.  
  28.         #list for all points
  29.         allPoints = []
  30.         allPointsCleaned = []
  31.  
  32.         #list for each cell of the raster
  33.         points00, points01, points10, points11 = [], [], [], []
  34.  
  35.         #list for the resulting points
  36.         resultPoints = []
  37.  
  38.         #minimal distance to be allowed between points
  39.         global minDist
  40.         minDist = self.Input2.getValue()
  41.  
  42.         #open the shapefile
  43.         shp = ogr.Open(self.Input1.getValue())
  44.         if shp is None:
  45.             return "failed to open shape"
  46.  
  47.         #get the first layer
  48.         lyr = shp.GetLayer(0)
  49.         lyr.ResetReading()
  50.  
  51.         #copy all points to the list
  52.         self.pointsToList(lyr, allPoints, allPointsCleaned)
  53.  
  54.         #calculate the bbox of the dataset
  55.         self.calcRange(lyr)
  56.  
  57.         #fill the raster cells with the corresponding points (5 % overlapp added)
  58.         self.fillCells(allPointsCleaned, points00, points01, points10, points11)
  59.  
  60.         #calculate the distance between the points of each cell, copy points with enough distance to all others into the result list
  61.         self.calcDist(points00, resultPoints)
  62.         self.calcDist(points01, resultPoints)
  63.         self.calcDist(points10, resultPoints)
  64.         self.calcDist(points11, resultPoints)
  65.  
  66.         #create new point geoemtry from the resulting points
  67.         pointGeom = ogr.Geometry(ogr.wkbMultiPoint)
  68.         newPoint = ogr.Geometry(ogr.wkbPoint)
  69.         for point in resultPoints:
  70.             newPoint.AddPoint(point[0], point[1], point[2])
  71.             pointGeom.AddGeometry(newPoint)
  72.  
  73.         wkt = pointGeom.ExportToWkt()
  74.  
  75.         #calculate elapsed time
  76.         elapsedTime = time.clock() - startTime
  77.  
  78.         self.Output1.setValue(wkt)
  79.         self.Output2.setValue(lowDists)
  80.         return
  81.  
  82.  
  83.     def pointsToList(self, lyr, allPoints, allPointsCleaned):
  84.         #iterate over all features of the layer
  85.         for feat in lyr:
  86.             #get the polygon geometry
  87.             geom = feat.GetGeometryRef()
  88.             #extracting the exterior ring
  89.             exRing = geom.GetGeometryRef(0)
  90.             #get the exterior rings point count
  91.             points = exRing.GetPointCount()
  92.            
  93.             #iterate over the points and put them in the list
  94.             for p in range(points):
  95.                 x, y, z = exRing.GetPoint(p)
  96.                 point = x, y, z
  97.                 allPoints.append(point)
  98.        
  99.         #delete the last point of each triangle (we only need three point for our calculations)
  100.         for i in range(3, len(allPoints), 4):
  101.             allPointsCleaned.append(allPoints[i-3])
  102.             allPointsCleaned.append(allPoints[i-1])
  103.             allPointsCleaned.append(allPoints[i-1])
  104.  
  105.  
  106.         return
  107.  
  108.     def calcRange(self, lyr):
  109.         extent = lyr.GetExtent()
  110.         boundary = str(extent).split(",")
  111.  
  112.         global bbox_xmin, bbox_xmax, bbox_ymin, bbox_ymax
  113.         bbox_xmin = boundary[0]
  114.         bbox_xmax = boundary[1]
  115.         bbox_ymin = boundary[2]
  116.         bbox_ymax = boundary[3]            
  117.         #delete useless characters
  118.         bbox_xmin = bbox_xmin.replace("(","")
  119.         bbox_ymax = bbox_ymax.replace(")","")
  120.  
  121.  
  122.     def fillCells(self, inputPoints, points00, points01, points10, points11):
  123.         #fill the cells with the corresponding points
  124.         for point in inputPoints:
  125.             pointString = str(point)
  126.             pointString = pointString.replace("(", "")
  127.             pointString = pointString.replace(")", "")    
  128.             x, y, z = pointString.split(",")
  129.             x, y, z = float(x), float(y), float(z)
  130.             pointAsFloat = x, y, z
  131.            
  132.             #cell00
  133.             if(x < float(bbox_xmax) - ((float(bbox_xmax) - float(bbox_xmin))/2) and y < float(bbox_ymax) - ((float(bbox_ymax) - float(bbox_ymin))/2)):
  134.                 points00.append(pointAsFloat)
  135.             #cell01
  136.             elif(x > float(bbox_xmax) - ((float(bbox_xmax) - float(bbox_xmin))/2) and y < float(bbox_ymax) - ((float(bbox_ymax) - float(bbox_ymin))/2)):
  137.                 points01.append(pointAsFloat)
  138.             #cell10    
  139.             elif(x < float(bbox_xmax) - ((float(bbox_xmax) - float(bbox_xmin))/2) and y > float(bbox_ymax) - ((float(bbox_ymax) - float(bbox_ymin))/2)):
  140.                 points10.append(pointAsFloat)
  141.             #cell11
  142.             elif(x > float(bbox_xmax) - ((float(bbox_xmax) - float(bbox_xmin))/2) and y > float(bbox_ymax) - ((float(bbox_ymax) - float(bbox_ymin))/2)):
  143.                 points10.append(pointAsFloat)    
  144.         return
  145.  
  146.     def calcDist(self, points, resultPoints):
  147.         #boolean for marking a point for as valid or not valid
  148.         valid = True
  149.         #calculate the distance to all other points
  150.         for pStart in points:
  151.             valid = True
  152.             for pAim in points:
  153.                 dist = ((pStart[0] - pAim[0])**2 + (pStart[1] - pAim[1])**2 + (pStart[2] - pAim[2])**2)
  154.                 #avoid comparision with point to itsself
  155.                 if dist < minDist and dist != 0.0:
  156.                     valid = False
  157.                     #lowDists.append(dist)
  158.             if valid:
  159.                 #avoid duplicates
  160.                 if pStart not in resultPoints:
  161.                     resultPoints.append(pStart)  
  162.  
  163.  
  164.         return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement