-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpymwv.py
134 lines (121 loc) · 5.34 KB
/
pymwv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""MIT License
Copyright (c) 2018 Mauricio Carvalho Mathias de Paulo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE."""
from osgeo import ogr
import math, sys
class MWVD():
def ApoloniusCircle(self,s1,s2,w1,w2, extent): #ogrpoint s1,s2, double w1,w2
#Aurenhammer's formulae
s1x=s1.GetX()
s1y=s1.GetY()
s2x=s2.GetX()
s2y=s2.GetY()
if(w1==w2): #regular voronoi
mx=(s1x+s2x)/2.
my=(s1y+s2y)/2.
dx=s1x-s2x
dy=s1y-s2y
d=extent.GetBoundary().Length()
curve=ogr.Geometry(ogr.wkbLineString)
if (dy!=0):
m=math.atan(-1.*(dx/dy))
curve.AddPoint_2D(-d*math.cos(m)+mx,-d*math.sin(m)+my)
curve.AddPoint_2D(d*math.cos(m)+mx,d*math.sin(m)+my)
else:
curve.AddPoint_2D(mx,-d+my)
curve.AddPoint_2D(mx,d+my)
b=extent.GetBoundary()
shortCurve=curve.Intersection(extent)
diff=b.Difference(curve)
boundary=diff.GetGeometryRef(1)
endPoint=boundary.GetPoint(0)
boundary.AddPoint_2D(endPoint[0], endPoint[1])
ring=ogr.Geometry(ogr.wkbLinearRing)
for point in boundary.GetPoints():
ring.AddPoint_2D(point[0], point[1])
ring.AddGeometry(boundary)
domBoundary=ogr.Geometry(ogr.wkbPolygon)
domBoundary.AddGeometry(ring)
else: #weighted voronoi
den=1./(w1*w1-w2*w2);
cx=(w1*w1*s2x-w2*w2*s1x)*den
cy=(w1*w1*s2y-w2*w2*s1y)*den
#print('Center:', cx, cy)
d= math.sqrt(((s1x-s2x)*(s1x-s2x) + (s1y-s2y)*(s1y-s2y)))
r=w1*w2*d*den
#print("Radius:",r)
if (r<0): r=r*-1
#creating the circle boundary from 3 points
arc= ogr.Geometry(ogr.wkbCircularString)
arc.AddPoint_2D(cx+r,cy)
arc.AddPoint_2D(cx-r,cy)
arc.AddPoint_2D(cx+r,cy)
#creating the circle polygon
domBoundary=ogr.Geometry(ogr.wkbCurvePolygon)
domBoundary.AddGeometry(arc)
if s1.Intersects(domBoundary):
return domBoundary.Intersection(extent)
else:
return extent.Difference(domBoundary)
def getMWVLayer(self, sites, outDS, layerName, extent):
outLayer = outDS.CreateLayer(layerName, geom_type=ogr.wkbPolygon )
for site1 in sites:
dominance=extent
for site2 in sites:
if site1!=site2:
twoSitesDominance=self.ApoloniusCircle(site1['p'], site2['p'], site1['w'], site2['w'], extent)
dominance=dominance.Intersection(twoSitesDominance)
# Get the output Layer's Feature Definition
featureDefn = outLayer.GetLayerDefn()
# create a new feature
outFeature = ogr.Feature(featureDefn)
# Set new geometry
outFeature.SetGeometry(dominance.GetLinearGeometry())
# Add new feature to output Layer
outLayer.CreateFeature(outFeature)
def readSitesFromLayer(self, ds, layerName, weightAttribute):
layer=ds.GetLayerByName(layerName)
sites=[]
for feature in layer:
w=feature.GetFieldAsDouble(weightAttribute)
p=feature.GetGeometryRef()
sites.append({'p':p.Clone(), 'w':w})
return sites
def getLayerExtent(self, ds, layerName):
layer=ds.GetLayerByName(layerName)
extent=ogr.Geometry( ogr.wkbPolygon)
lring=ogr.Geometry( ogr.wkbLinearRing)
le=layer.GetExtent()
lring.AddPoint(le[0], le[0])
lring.AddPoint(le[0], le[3])
lring.AddPoint(le[1], le[3])
lring.AddPoint(le[1], le[0])
lring.AddPoint(le[0], le[0])
extent.AddGeometry(lring)
d=abs(le[0]-le[2])
extent=extent.Buffer(d/10., 0)
return extent
if __name__=="__main__":
if (len(sys.argv) !=5):
print("USAGE:")
print("python pymwv.py ogrDataSource SitesLayerName WeightAttribute OutpuLayerName")
else:
runObj=MWVD()
outDS=ogr.Open(sys.argv[1], 1)
sites=runObj.readSitesFromLayer(outDS, sys.argv[2], sys.argv[3])
extent=runObj.getLayerExtent(outDS, sys.argv[2])
runObj.getMWVLayer(sites, outDS, sys.argv[4], extent)