This file is indexed.

/usr/share/pyshared/Epigrass/epigdal.py is in epigrass 2.0.4-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
This module uses the GDAL and OGR Library to read maps in vaious formats and
export the results of Epigrass simulations to the formats supported by these libraries

copyright 2007 by Flavio Codeco Coelho
Licensed under the GPL.
"""
import locale, os#, pylab
from osgeo  import ogr,  gdal
from xml.dom import minidom, Node
from matplotlib.colors import  rgb2hex, LogNorm
from matplotlib import cm
class World:
    def __init__(self,filename,namefield='name',geocfield='geocode',outdir = '.'):
        '''
        Instantiate a world object.
        Filename points to a file supported by OGR
        '''
        self.geocfield = geocfield
        self.namefield = namefield
        self.outdir = outdir
        self.ds =ogr.Open(filename)
        self.name = self.ds.GetName()
        self.driver = self.ds.GetDriver()
        self.centroids = []#centroid list (x,y,z) tuples
        self.centdict = {} #keys are geocode, values are (x,y,z) tuples
        self.geomdict = {} #keys are geocode, values are geometries
        self.nlist = [] #nodelist: feature objects
        self.nodesource = False #True if Node datasource has been created
        self.edgesource = False #True if Edge datasource has been created
        self.datasource = False #True if Data datasource has been created
        self.layerlist = self.getLayerList()
    def getLayerList(self):
        """
        returns a list with the
        available layers by name
        """
        nlay = self.ds.GetLayerCount()
        ln = []
        for i in xrange(nlay):
            l = self.ds.GetLayer(i)
            ln.append(l.GetName())
        return ln

    def drawLayer(self,L):
        '''
        Draws a polygon layer using pylab
        '''
        N = 0
        L.ResetReading()
        feat = L.GetNextFeature()
        while feat is not None:
            field_count = L.GetLayerDefn().GetFieldCount()
            geo = feat.GetGeometryRef()
            if geo.GetGeometryCount()<2:
                g1 = geo.GetGeometryRef( 0 )
                x =[g1.GetX(i) for i in range(g1.GetPointCount()) ]
                y =[g1.GetY(i) for i in range(g1.GetPointCount()) ]
                pylab.plot(x,y,'-',hold=True)
            for c in range( geo.GetGeometryCount()):
                        ring = geo.GetGeometryRef ( c )
                        for cnt in range( ring.GetGeometryCount()):
                            g1 = ring.GetGeometryRef( cnt )
                            x =[g1.GetX(i) for i in range(g1.GetPointCount()) ]
                            y =[g1.GetY(i) for i in range(g1.GetPointCount()) ]
                            pylab.plot(x,y,'-',hold=True)
            feat = L.GetNextFeature()
        pylab.xlabel("Longitude")
        pylab.ylabel("Latitude")
        pylab.grid(True)
        pylab.show()

    def getNodeList(self,l):
        '''
        Returns the centroid coordinates
        l is an OGR layer.
        '''
        self.nlist=[]
        f = l.GetNextFeature()
        while f is not None:
            g = f.GetGeometryRef()
            self.geomdict[f.GetFieldAsInteger(self.geocfield)] = g
            try:
                c = g.Centroid()
                self.nlist.append(f)
                self.centdict[f.GetFieldAsInteger(self.geocfield)] = (c.GetX(),c.GetY(),c.GetZ())
            except: #in case feature is not a polygon
                print  f.GetFID(),g.GetGeometryType()
            f = l.GetNextFeature()
        #print (2600501 in self.centdict)


    def createNodeLayer(self):
        """
        Creates a new shape file to represent network nodes.
        The node layer will be based on the centroids of the
        polygons belonging to the map layer associated with this
        world instance.
        namefield - name of the field containing polygon name
        geocfield - name of the field containing geocode
        """
        # Creates a new shape file to hold the data
        if os.path.exists(os.path.join(self.outdir,'Nodes.shp')):
            os.remove(os.path.join(self.outdir,'Nodes.shp'))
            os.remove(os.path.join(self.outdir,'Nodes.shx'))
            os.remove(os.path.join(self.outdir,'Nodes.dbf'))
        if not self.nodesource:
            dsn = self.driver.CreateDataSource(os.path.join(self.outdir,'Nodes.shp'))
            self.nodesource = dsn
            nl = dsn.CreateLayer("nodes",geom_type=ogr.wkbPoint)
        #Create the fields
        fi1 = ogr.FieldDefn("name")
        fi2 = ogr.FieldDefn("geocode",field_type=ogr.OFTInteger)
        fi3 = ogr.FieldDefn("x",field_type=ogr.OFTString)
        fi4 = ogr.FieldDefn("y",field_type=ogr.OFTString)
        nl.CreateField(fi1)
        nl.CreateField(fi2)
        nl.CreateField(fi3)
        nl.CreateField(fi4)
        #Add the features (points)
        for f in self.nlist:
            gc = f.GetFieldAsInteger(self.geocfield)
            x = self.centdict[gc][0]
            y = self.centdict[gc][1]
            fe = ogr.Feature(nl.GetLayerDefn())
            name = f.GetField(self.namefield)
            fe.SetField('name',f.GetField(self.namefield))
            fe.SetField('geocode',gc)
            fe.SetField('x',str(x))
            fe.SetField('y',str(y))
            pt = ogr.Geometry(type=ogr.wkbPoint)
            pt.AddPoint(x,y)
            fe.SetGeometryDirectly(pt)
            nl.CreateFeature(fe)
        nl.SyncToDisk()

    def createEdgeLayer(self,elist):
        """
        Creates a new layer with edge information.
        elist is a list of tuples:
        (sgeoc,dgeoc,fsd,fds)
        """
        # Creates a new shape file to hold the data
        if os.path.exists(os.path.join(self.outdir,'Edges.shp')):
            os.remove(os.path.join(self.outdir,'Edges.shp'))
            os.remove(os.path.join(self.outdir,'Edges.shx'))
            os.remove(os.path.join(self.outdir,'Edges.dbf'))
        if not self.edgesource:
            dse = self.driver.CreateDataSource(os.path.join(self.outdir,'Edges.shp'))
            self.edgesource = dse
            el = dse.CreateLayer("edges",geom_type=ogr.wkbLineString)
        #Create the fields
        fi1 = ogr.FieldDefn("source_geocode",field_type=ogr.OFTInteger)
        fi2 = ogr.FieldDefn("dest_geocode",field_type=ogr.OFTInteger)
        fi3 = ogr.FieldDefn("flowSD",field_type=ogr.OFTReal)
        fi3.SetPrecision(12)
        fi4 = ogr.FieldDefn("flowDS",field_type=ogr.OFTReal)
        fi4.SetPrecision(12)
        el.CreateField(fi1)
        el.CreateField(fi2)
        el.CreateField(fi3)
        el.CreateField(fi4)
        #Add the features (lines)
        for e in elist:
            #print "setting edge fields"
            fe = ogr.Feature(el.GetLayerDefn())
            fe.SetField('source_geocode',e[0])
            fe.SetField('dest_geocode',e[1])
            fe.SetField('flowSD',e[2])
            fe.SetField('flowSD',e[3])
            line = ogr.Geometry(type=ogr.wkbLineString)
            try:
                #print "creating edge lines"
                line.AddPoint(self.centdict[int(e[0])][0],self.centdict[int(e[0])][1])
                line.AddPoint(self.centdict[int(e[1])][0],self.centdict[int(e[1])][1])
                fe.SetGeometryDirectly(line)
                el.CreateFeature(fe)
            except: #node not in centdict
                pass
        el.SyncToDisk()

    def createDataLayer(self,varlist, data):
        """
        Creates a new shape to contain data about nodes.
        varlist is the list of fields names associated with
        the nodes.
        data is a list of lists whose first element is the geocode
        and the remaining elements are values of the fields, in the
        same order as they appear in varlist.
        """
        if os.path.exists(os.path.join(self.outdir,'Data.shp')):
            os.remove(os.path.join(self.outdir,'Data.shp'))
            os.remove(os.path.join(self.outdir,'Data.shx'))
            os.remove(os.path.join(self.outdir,'Data.dbf'))
        # Creates a new shape file to hold the data
        if not self.datasource:
            dsd = self.driver.CreateDataSource(os.path.join(self.outdir,'Data.shp'))
            self.datasource = dsd
            dl = dsd.CreateLayer("sim_results",geom_type=ogr.wkbPolygon)
        #Create the fields
        fi1 = ogr.FieldDefn("geocode",field_type=ogr.OFTInteger)
        dl.CreateField(fi1)
        for v in varlist:
            #print "creating data fields"
            fi = ogr.FieldDefn(v,field_type=ogr.OFTString)
            fi.SetPrecision(12)
            dl.CreateField(fi)

        #Add the features (points)
        for n,l in enumerate(data):
            #Iterate over the lines of the data matrix.
            gc = l[0]
            try:
                geom = self.geomdict[gc]
                if geom.GetGeometryType() != 3: continue
                #print geom.GetGeometryCount()
                fe = ogr.Feature(dl.GetLayerDefn())
                fe.SetField('geocode',gc)
                for v,d in zip (varlist,l[1:]):
                    #print v,d
                    fe.SetField(v,str(d))
                #Add the geometry
                #print "cloning geometry"
                clone = geom.Clone()
                #print geom
                #print "setting geometry"
                fe.SetGeometry(clone)
                #print "creating geom"
                dl.CreateFeature(fe)
            except: #Geocode not in polygon dictionary
                pass
            dl.SyncToDisk()

    def genSitesFile(self,fname):
        """
        This method generate a sites
        csv file from the nodes extracted from the
        map.
        """
        f = open(fname,"w")
        for fe in self.nlist:
            gc = fe.GetFieldAsInteger(self.geocfield)
            x = self.centdict[gc][0]
            y = self.centdict[gc][1]
            name = fe.GetField(self.namefield)
            line = "%s,%s,%s,%s\n"%(x,y,name,gc)
            #fe.SetField('name',f.GetField(self.namefield))
            f.write(line)
        f.close()


    def closeSources(self):
        """
        Close the data sources so that data is flushed and and files are closed
        """
        if self.nodesource:
            self.nodesource.Destroy()
            #print "closed node files"
        if self.edgesource:
            self.edgesource.Destroy()
            #print "closed edge files"
        if self.datasource:
            self.datasource.Destroy()
            #print "closed data files"


class KmlGenerator:
    """
        Generate a KML file for displaying data on 
        Google Maps/Earth
    """
    def __init__(self):
        self.doc = None
        self.dnode = None #document node in the DOM
        self.genRoot()
    def genRoot(self):
        """
        Generate a KML file root.
        """
        self.kmldoc = doc = minidom.Document()
        kml = doc.createElement("kml")
        kml.setAttribute("xmlns","http://earth.google.com/kml/2.1")
        doc.appendChild(kml)
        d = doc.createElement("Document")
        kml.appendChild(d)
        nel = doc.createElement("name")
        name = doc.createTextNode("KML Epigrass data file")
        nel.appendChild(name)
        d.appendChild(nel)
        desc = doc.createElement("description")
        d.appendChild(desc)
        desc.appendChild(doc.createTextNode("Polygons with data"))
        self.dnode = d

    def addNodes(self,layer,names=None):
        """
        Adds a node to the document.
        d is the document element KML dom object.
        layer is a layer with polygons
        names is a dictionary of names indexed by geocode(int)
        """
        doc = self.dnode
        jet  = cm.get_cmap("jet",50)
        layer.ResetReading()
        while 1:
            f = layer.GetNextFeature()
            if not f:#exit after the last feature
                break
            prevalence = float(f.GetField("prevalence"))
            rgba = jet(prevalence)
            bgrcol = list(rgba[:-1]) #rgb(list)
            bgrcol.reverse() #turn it into bgr
            hexcol = "#80"+rgb2hex(bgrcol)[1:] #abgr Alpha set to 128
            g = f.GetGeometryRef()
            if g.GetGeometryType() == 3:
                if not names:
                    name = ""
                else:
                    try:
                        name = names[f.GetFieldAsInteger("geocode")]
                    except KeyError:
                        print f.GetFieldAsInteger("geocode")
                        name = ""
                description = "Prevalence: %s;\nTotal cases: %s;\nImported Cases: %s;"%(prevalence,f.GetField("totalcases"),f.GetField("arrivals"))
                locale.setlocale(locale.LC_ALL,"C") #avoids conversion of decimal points
                gml = g.ExportToGML() #extract the coordinates from the GML representation
                coords = gml.split('<gml:coordinates>')[1].split('</gml:coordinates>')[0]
                coords  = " ".join([i+",0" for i in coords.split(" ")]) #add z coordinate
                #create the kml elements
                #placemark and sub elements
                pm=self.kmldoc.createElement("Placemark")
                nm = self.kmldoc.createElement("name")
                nm.appendChild(self.kmldoc.createTextNode(name))
                desc = self.kmldoc.createElement("description")
                desc.appendChild(self.kmldoc.createTextNode(description))
                pm.appendChild(nm)
                pm.appendChild(desc)
                #style and subelements
                st=self.kmldoc.createElement("Style")
                pm.appendChild(st)
                ps = self.kmldoc.createElement("PolyStyle")
                color = self.kmldoc.createElement("color")
                color.appendChild(self.kmldoc.createTextNode(hexcol))
                ps.appendChild(color)
                fill = self.kmldoc.createElement("fill")
                fill.appendChild(self.kmldoc.createTextNode("1"))
                ps.appendChild(fill)
                outline = self.kmldoc.createElement("outline")
                outline.appendChild(self.kmldoc.createTextNode("1"))
                ps.appendChild(outline)
                st.appendChild(ps)
                doc.appendChild(pm)
                #Multigeometry
                mg = self.kmldoc.createElement("MultiGeometry")
                pm.appendChild(mg)
                polygon = self.kmldoc.createElement("Polygon")
                mg.appendChild(polygon)
                ob = self.kmldoc.createElement("outerBoundaryIs")
                polygon.appendChild(ob)
                linr = self.kmldoc.createElement("LinearRing")
                ob.appendChild(linr)
                coordin = self.kmldoc.createElement("coordinates")
                linr.appendChild(coordin)
                coordin.appendChild(self.kmldoc.createTextNode(coords))
    def writeToFile(self,dir):
        """
        Writes the kml file to disk
        """
        fullpath = os.path.join(dir,"Data.kml")
        f=open(fullpath,"w")
        f.write(self.kmldoc.toxml())
        f.close()

if __name__=="__main__":
    # opening data source
    w = World('riozonas_LatLong.shp','nome_zonas','zona_trafe')
    layer = w.ds.GetLayerByName(w.layerlist[0])
    w.getNodeList(layer)
    w.drawLayer(layer)
    w.createNodeLayer()
    w.nodesource.Destroy() #flush data to disk
##    k = KmlGenerator()
##    k.addNodes(w.datasource.GetLayer(0))
##    k.writeToFile()