def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg) 

        try:
            #nodeFile = argv[1] # Nodes Shape File
            #nodeFile=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\AllPointsIncludingFakes.shp"
            nodeFile=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\AllNodesMerged_Updated.shp"
            #OutputDir = argv[2] # Directory for texts and final network
            OutputDir=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\75"
            #existingTree=argv[3]
            #existingTree=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\Backbone1\network.shp"
            existingTree=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\backBone\network.shp"
            searchRadius = "1500 meters"
            #numNodes=8912

        except IndexError:
            raise Error("Not enough arguments provided to script.")
        
        nodes=readNodesFromShp(nodeFile)
        numNodes=len(nodes)
        net=fileRWarcgis.readNetFromShpUsingNodes(existingTree, nodes)
        backBoneNodes=net.numNodes()
        print backBoneNodes, numNodes

                
        generateDistFromNodeDict(nodes,nodeFile,OutputDir, searchRadius) ### bu functiona dikkat!!!proj file!!!
        
        shapePath = OutputDir + os.sep + "ExpandedNetwork.shp"
        distFileLV = OutputDir + os.sep + "dists.txt"
        nodeFileLV = OutputDir + os.sep + "nodes.txt"
        projFile = OutputDir + os.sep + "shpProj.prj"
        testSegmentsForLV=fileRW.readSegsFromTxt(distFileLV, nodes)
        segmentsByNodeID=buildAssocDict(testSegmentsForLV, excludedNodes=[])
        resultsFile = OutputDir + os.sep + "results.txt"
        ofile = open(resultsFile, "w")
        ofile.write("PenetrationRate NodesConnected Total Lenght\n")
        for i in range(10,11):
            penetrationRate=0.05*i
            finalTree=primsAlgforLV(nodes,net,numNodes, segmentsByNodeID,(backBoneNodes+penetrationRate*(numNodes-backBoneNodes)),excludedNodes=[])
            totalCost=finalTree.getTotalEdgeWeight()
            nodesConnected=finalTree.numNodes()
            ofile.write("%(penetrationRate)f %(nodesConnected)i %(totalCost)f \n" %vars())  
        
        ofile.write("END\n")
        ofile.close()
        
        print finalTree.getTotalEdgeWeight()   
        fileRW.genShapefile(finalTree, projFile, shapePath)
コード例 #2
0
def runAlgorithm(algorithm, inputDir, outputDir,centerNumber, cutoff=None):
    if not os.path.exists(outputDir):
        try:
            os.mkdir(outputDir)
        except:
            raise Error("Could not create new directory " + inputFile)
    distFile = inputDir + os.sep + "dists.txt"
    nodeFile = inputDir + os.sep + "nodes.txt"
    projFile = inputDir + os.sep + "shpProj.prj"
    shapePath = outputDir + os.sep + "network.shp"
    if not os.path.exists(inputDir):
        print inputDir + " does not exist"
        return 1
    elif not os.path.exists(outputDir): 
        print outputDir + " does not exist"
        return 1
    elif not os.path.exists(distFile): 
        print distFile + " does not exist"
        return 1
    elif not os.path.exists(nodeFile): 
        print nodeFile + " does not exist"
        return 1
    elif not os.path.exists(projFile): 
        print projFile + " does not exist"
        return 1

    print "Generating network hehehh:"

    nodes = fileRW.readNodesFromTxt(nodeFile)
    testSegments = fileRW.readSegsFromTxt(distFile, nodes, cutoff)

    numNodes = len(nodes)
    
    print "\tCalculating tree..."
    
    tree = kruskalForCluster(testSegments, numNodes,centerNumber)

    print "\tNodes:\t\t" + str(tree.numNodes())
    print "\tConnections:\t" + str(tree.numEdges())
    print "\tSubnet(s):\t\t" + str(tree.numSubnets())

    # Alex says sum benefit function over tree to get total benefit for tree
    # Can compare trees using this to pick best from set of trees
    print "\tTotal Weight:\t" + str(tree.getTotalEdgeWeight())

    if tree.numSubnets() == 1:
        MID = tree.getTotalEdgeWeight() / tree.getTotalNodeWeight()
        print "\tMID (100):\t\t" + str(MID)
    else:
        print "\tMultiple subnets. No MID calculated."

    print "Generating network shapefile..."
    fileRWReturnCode = fileRW.genShapefile(tree, projFile, shapePath)
    if fileRWReturnCode == 0:
        print "\tNetwork saved as shapefile at: " + shapePath
        return 0
    else:
       return 1
コード例 #3
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)

        #if len(args) == 0:
        #raise Usage("No arguments specified.")
        try:
            #inputShapeFile = argv[1]
            #inputShapeFile=r"C:\Documents and Settings\Selin\Desktop\Mbola\MbolaPts.shp"
            inputShapeFile = r"C:\Documents and Settings\Selin\Desktop\MicroGrid_5march\MbolaN.shp"
            #inputShapeFile=r"C:\Documents and Settings\Selin\Desktop\sampleDir\Pampaida\PampaidaPts.shp"
            #outputDir = argv[2]
            outputDir = r"C:\Documents and Settings\Selin\Desktop\MicroGrid_5march\MbolaN"
            #algorithm = argv[3]
            searchRadius = "300 meters"
        except IndexError:
            raise Error("Not enough arguments provided to script.")
        startTime = time.time()

        nodesByClusterID, clusterByNode, nodes, centers, LVCostDict = generateDictsFromShp(
            inputShapeFile, outputDir)

        totalCost, tree, centers, nodesByClusterID = run(
            centers, nodesByClusterID, clusterByNode, searchRadius)

        statsFile1 = outputDir + os.sep + "ClusterSize.txt"
        statsFile2 = outputDir + os.sep + "Centers.txt"
        writeCentersToText(statsFile1, statsFile2, centers)

        TreeLV = network.Network()

        TreeWithLV = addLVSeg(TreeLV, centers, nodesByClusterID)

        fileRW.genShapefile(TreeWithLV, outputDir + ".prj",
                            outputDir + os.sep + "LV.shp")
        finishTime = time.time()
        print "Running Time:", finishTime - startTime
コード例 #4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        try:
            #inputShapeFile = argv[1]

            inputShapeFile = r"C:\Users\Selin\Desktop\DATA\real\Bonsaaso\BonsaasoRandPts.shp"
            #outputDir = argv[2]
            outputDir = r"C:\Users\Selin\Desktop\DATA\real"
            #outputDir=r"/home/selin/Desktop/rsSep17/200"
            searchRadius = 50000
            distFromT = 100

        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()
        print "Generating Dictionaries"

        nodesByClusterID, clusterByNode, nodes, centers = generateDictsFromShp(
            inputShapeFile, outputDir)

        print "Run function starts..."
        timeBeforeRun = time.time()
        #totalCost,tree,centers,nodesByClusterID,LVCostDict=run(centers,nodesByClusterID,clusterByNode,LVCostDict,searchRadius,MV,LV,TCost,distFromT,outputDir)
        centers, nodesByClusterID = run(centers, nodesByClusterID,
                                        clusterByNode, searchRadius, distFromT,
                                        outputDir)
        print "Time for RUN:", time.time() - timeBeforeRun

        statsFile = outputDir + os.sep + "Center_StarConfig_100.csv"
        csvWriter = csv.writer(open(statsFile, 'wb'))
        outFile = open(statsFile, "w")

        TreeLV = network.Network()

        TreeWithLV, meanSeg = addLVSeg(TreeLV, centers, nodesByClusterID)
        fileRW.genShapefile(TreeWithLV, outputDir + ".prj",
                            outputDir + os.sep + "StarConfig_100.shp")

        for center in centers.values():
            #print nodesByClusterID[center.getID()]
            centersDict = defaultdict(list)

            for node in nodesByClusterID[center.getID()]:
                centersDict[node.getID()] = node
            segments = generateSegments(centersDict, searchRadius)
            MID = kruskalsAlgForMID(
                segments, centersDict
            )  # buraya iyice bir bak tam istedigimi HALA yapmior olabilir.
            x = center.getX()
            y = center.getY()
            size = center.getWeight()
            del centersDict
            MeanSegment = meanSeg[center.getID()]

            csvWriter.writerow([x, y, size, MID, MeanSegment])

            #outFile.write("%(x)f \n" %vars())

#outFile.write("%(y)f \n" %vars())
#outFile.write("%(size)i \n" %vars())
#outFile.write("%(MID)f \n" %vars())

        outFile.close()

        numTransformer = len(centers)
        afterrun = time.clock()

        print "Num Transformers", numTransformer
        print "Total Running Time:", time.time() - startTime
コード例 #5
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        if len(args) == 0:
            raise Usage("No arguments specified.")

        inputShapefile = argv[1]
        distancesDir = argv[2]
        startPtShapefile = argv[3]
        maxNodes = float(argv[4])
        subnetIDFieldname = argv[5]
        orderFieldname = argv[6]

        distFile = distancesDir + os.sep + "dists.txt"
        nodeFile = distancesDir + os.sep + "nodes.txt"
        nodes = fileRW.readNodesFromTxt(nodeFile)
        segs = fileRW.readSegsFromTxt(distFile, nodes)
        
        gp.addMessage("Reading starting point...")
        rows = gp.searchCursor(startPtShapefile)
        desc = gp.describe(startPtShapefile)
        row = rows.next()
        feat = row.GetValue(desc.ShapeFieldName)
        FID = row.getValue("FID")
        point = feat.getPart(0)
        startNode = network.Node(FID, point.x, point.y, None)
        del rows

        nodeDict = buildAssocDict(segs)

        gp.addMessage("Calculating subnets...")
        nodeOrder = {}
        subNet = {}
        distList = makeDistList(startNode, nodes)
        n = 0
        while len(segs) > 0:
            print n
            # Choose first node
            firstNodeID = distList[0].getNode()
            tree = primsAlg(segs, maxNodes, firstNodeID, nodeDict)
            m = 0
            for node in tree.getNodes():
                nodeID = node.getID()
                print nodeID
                nodeOrder[nodeID] = m
                subNet[nodeID] = n
                distList.remove(distance(None,nodeID))
                for seg in nodeDict[node.getID()]:
                    for node in [seg.getNode1(), seg.getNode2()]:
                        nodeDict[node.getID()].remove(seg)
                m += 1
            n += 1
            if n == 5:
                break

        gp.addMessage("Writing data to shapefile...")
        # Copy projection information to shpProj.prj file for later use
        rootDir, fc = os.path.split(inputShapefile)
        base, ext =  os.path.splitext(fc)
        projFile = rootDir + os.sep + base + ".prj"
        outputShapefile = rootDir + os.sep + base + "nets" + ".shp"
        fileRW.genShapefile(tree, projFile, outputShapefile)
コード例 #6
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        try:
            #inputShapeFile = argv[1]
            
            inputShapeFile=r"C:\Users\Selin\Desktop\DATA\real\Bonsaaso\BonsaasoRandPts.shp"
            #outputDir = argv[2]
            outputDir=r"C:\Users\Selin\Desktop\DATA\real"
            #outputDir=r"/home/selin/Desktop/rsSep17/200"
            searchRadius = 50000
            distFromT=100
            
        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()
        print "Generating Dictionaries"
       
        nodesByClusterID,clusterByNode,nodes,centers=generateDictsFromShp(inputShapeFile,outputDir)
       
        print "Run function starts..."
        timeBeforeRun=time.time()
        #totalCost,tree,centers,nodesByClusterID,LVCostDict=run(centers,nodesByClusterID,clusterByNode,LVCostDict,searchRadius,MV,LV,TCost,distFromT,outputDir)
        centers,nodesByClusterID=run(centers,nodesByClusterID,clusterByNode,searchRadius,distFromT,outputDir)
        print "Time for RUN:", time.time()-timeBeforeRun
                        
      
        

        statsFile= outputDir + os.sep + "Center_StarConfig_100.csv"
        csvWriter = csv.writer(open(statsFile, 'wb'))
        outFile = open(statsFile,"w")
        
        TreeLV=network.Network()

        TreeWithLV,meanSeg=addLVSeg(TreeLV,centers,nodesByClusterID)
        fileRW.genShapefile(TreeWithLV, outputDir + ".prj", outputDir + os.sep + "StarConfig_100.shp")
        
        
        for center in centers.values():
            #print nodesByClusterID[center.getID()]
            centersDict=defaultdict (list)
            
            for node in nodesByClusterID[center.getID()]:
                centersDict[node.getID()]=node
            segments=generateSegments(centersDict,searchRadius)
            MID=kruskalsAlgForMID(segments,centersDict) # buraya iyice bir bak tam istedigimi HALA yapmior olabilir.
	    x=center.getX()
	    y=center.getY()
	    size=center.getWeight()
            del centersDict
            MeanSegment=meanSeg[center.getID()]
	   
           
            csvWriter.writerow([x, y, size, MID,MeanSegment])


            #outFile.write("%(x)f \n" %vars())
	    #outFile.write("%(y)f \n" %vars())
            #outFile.write("%(size)i \n" %vars())
	    #outFile.write("%(MID)f \n" %vars())

        outFile.close()
	
	
                     
        
        numTransformer=len(centers)
        afterrun=time.clock()
     
        print "Num Transformers", numTransformer        
        print "Total Running Time:",time.time()-startTime
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)

        try:
            #nodeFile = argv[1] # Nodes Shape File
            #nodeFile=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\AllPointsIncludingFakes.shp"
            nodeFile = r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\AllNodesMerged_Updated.shp"
            #OutputDir = argv[2] # Directory for texts and final network
            OutputDir = r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\75"
            #existingTree=argv[3]
            #existingTree=r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\Backbone1\network.shp"
            existingTree = r"C:\Users\Selin\Documents\COLUMBIA\Project\for World Bank Prensentation\RuhiiraForVillageNetwork\MainGridExpansion\backBone\network.shp"
            searchRadius = "1500 meters"
            #numNodes=8912

        except IndexError:
            raise Error("Not enough arguments provided to script.")

        nodes = readNodesFromShp(nodeFile)
        numNodes = len(nodes)
        net = fileRWarcgis.readNetFromShpUsingNodes(existingTree, nodes)
        backBoneNodes = net.numNodes()
        print backBoneNodes, numNodes

        generateDistFromNodeDict(
            nodes, nodeFile, OutputDir,
            searchRadius)  ### bu functiona dikkat!!!proj file!!!

        shapePath = OutputDir + os.sep + "ExpandedNetwork.shp"
        distFileLV = OutputDir + os.sep + "dists.txt"
        nodeFileLV = OutputDir + os.sep + "nodes.txt"
        projFile = OutputDir + os.sep + "shpProj.prj"
        testSegmentsForLV = fileRW.readSegsFromTxt(distFileLV, nodes)
        segmentsByNodeID = buildAssocDict(testSegmentsForLV, excludedNodes=[])
        resultsFile = OutputDir + os.sep + "results.txt"
        ofile = open(resultsFile, "w")
        ofile.write("PenetrationRate NodesConnected Total Lenght\n")
        for i in range(10, 11):
            penetrationRate = 0.05 * i
            finalTree = primsAlgforLV(nodes,
                                      net,
                                      numNodes,
                                      segmentsByNodeID,
                                      (backBoneNodes + penetrationRate *
                                       (numNodes - backBoneNodes)),
                                      excludedNodes=[])
            totalCost = finalTree.getTotalEdgeWeight()
            nodesConnected = finalTree.numNodes()
            ofile.write(
                "%(penetrationRate)f %(nodesConnected)i %(totalCost)f \n" %
                vars())

        ofile.write("END\n")
        ofile.close()

        print finalTree.getTotalEdgeWeight()
        fileRW.genShapefile(finalTree, projFile, shapePath)
コード例 #8
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        if len(args) == 0:
            raise Usage("No arguments specified.")

        inputShapefile = argv[1]
        startPtShapefile = argv[2]
        outputShapefile = argv[3]
        nodeShapefile = argv[4]
        nodeShapefile1 = argv[5]
        lengthTable = argv[6]
        maxNodes = int(argv[7])

        gp.addMessage("Reading input nodes...")
        # Read nodes from input shapefile, creating network.Node instances.
        rows = gp.searchCursor(inputShapefile)
        row = rows.next()
        desc = gp.describe(inputShapefile)
        nodes = {}
        while row:
            feat = row.getValue(desc.ShapeFieldName)
            pnt = feat.getpart()
            # Read nodes.
            try:
                ptWeight = row.getValue("Weight")
            except:
                ptWeight = 1
            ptFID = row.getValue("FID")
            nodes[ptFID] = network.Node(ptFID, pnt.x, pnt.y, ptWeight)
            row = rows.next()
        del rows  # ensure cursor closes

        outputPath = os.path.dirname(outputShapefile)
        if not os.path.exists(outputPath):
            os.mkdir(outputPath)
        distFile = outputPath + os.sep + "dists.txt"
        nodeFile = outputPath + os.sep + "nodes.txt"

        # Copy projection information to shpProj.prj file for later use
        rootDir, fc = os.path.split(inputShapefile)
        base, ext = os.path.splitext(fc)
        projFile = rootDir + os.sep + base + ".prj"
        newProjFile = outputPath + os.sep + "shpProj.prj"
        try:
            if os.path.exists(newProjFile):
                os.remove(newProjFile)
            shutil.copy(projFile, newProjFile)
            projFile = newProjFile
        except WindowsError:
            gp.AddError("Could not find projection file")

        # Read starting point coordinates from the point in startPtShapefile
        rows = gp.searchCursor(startPtShapefile)
        desc = gp.describe(startPtShapefile)
        row = rows.next()
        feat = row.GetValue(desc.ShapeFieldName)
        FID = row.getValue("FID")
        point = feat.getPart(0)
        startNode = network.Node(FID, point.x, point.y, None)
        del rows

        firstNodeFID = nearestNode(startNode, nodes)

        gp.addMessage("Calculating tree...")
        tree = primsAlg(nodes, maxNodes, firstNodeFID)

        gp.addMessage("Writing output...")
        if os.path.exists(outputShapefile):
            gp.Delete_management(outputShapefile)
        fileRW.genShapefile(tree, projFile, outputShapefile)

        # Output a shapefile of the nodes in the network.
        if os.path.exists(nodeShapefile):
            gp.Delete_management(nodeShapefile)
        rootDir, fc = os.path.split(nodeShapefile)
        gp.CreateFeatureclass_management(rootDir, fc, "POINT")
        outDesc = gp.describe(nodeShapefile)
        shapefield = outDesc.ShapeFieldName
        origFIDField = "OrigFID"
        gp.addfield(nodeShapefile, origFIDField, "LONG")
        lengthField = "Length"
        gp.addfield(nodeShapefile, lengthField, "FLOAT")
        gp.deleteField_management(nodeShapefile, "Id")
        nodeRows = gp.insertCursor(nodeShapefile)
        point = gp.createobject("Point")
        for node in tree.getNodes():
            nodeFID = node.getID()
            sql = '"FID"=%i' % (nodeFID)
            rows = gp.searchCursor(inputShapefile, sql)
            oldRow = rows.next()
            row = nodeRows.newRow()
            row.setValue(origFIDField, nodeFID)
            row.setValue(lengthField, oldRow.getValue(lengthField))
            point.x = node.getX()
            point.y = node.getY()
            row.SetValue(shapefield, point)
            nodeRows.insertRow(row)
        del nodeRows
        del rows
        gp.defineprojection_management(nodeShapefile, projFile)
        if os.path.exists(nodeShapefile1):
            gp.Delete_management(nodeShapefile1)
        gp.CopyFeatures_management(nodeShapefile, nodeShapefile1)

        # Return a DBF with one field, the total length of the network.
        if os.path.exists(lengthTable):
            gp.Delete_management(lengthTable)
        rootDir, table = os.path.split(lengthTable)
        gp.CreateTable_management(rootDir, table)
        totalLenField = "totalLen"
        gp.addfield(lengthTable, totalLenField, "FLOAT")
        gp.deleteField(lengthTable, "Field1")
        rows = gp.insertCursor(lengthTable)
        row = rows.newRow()
        row.setValue(totalLenField, tree.getTotalEdgeWeight())
        rows.insertRow(row)
        del rows
コード例 #9
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        try:
            #inputShapeFile = argv[1]
           
            inputShapeFile=r"C:\Users\Selin\Desktop\sample\sample2.shp"
            
            #outputDir = argv[2]
            outputDir=r"C:\Users\Selin\Desktop\sample\sample2"
            #algorithm = argv[3]
            searchRadius =20000
            MV =10# Cost of MV per meter
            LV = 10 # Cost of LV per meter
            TCost=0 # Transformer Cost
            distFromT=2500
            
        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()
        print "Generating Dictionaries"
       
        nodesByClusterID,clusterByNode,nodes,centers,LVLenDict=generateDictsFromShp(inputShapeFile,outputDir)
       
        print "Run function starts..."
        timeBeforeRun=time.time()
        totalCost,tree,centers,nodesByClusterID,LVLenDict=run(centers,nodesByClusterID,clusterByNode,LVLenDict,searchRadius,MV,LV,TCost,distFromT,outputDir)
        print "Time for RUN:", time.time()-timeBeforeRun
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp")
        
        statsFile1= outputDir + os.sep + "LVCostDict.txt" 
	statsFile2= outputDir + os.sep + "CenterSize.txt" 
        writeLVDictToText(statsFile1, LVLenDict,LV)
        writeCenterSizeToText(statsFile2, centers)
        #batchPrimsforTransformers.batchPrims(tree,centers,LVLenDict,outputDir)
       
        print "LVLength_run", sum(LVLenDict.values())
        MVLength=tree.getTotalEdgeWeight()
        
        MVCost=MVLength*MV
        numTransformer=len(centers)
        afterrun=time.clock()
        '''Use below if multipoint LV lines are needed'''
        maxLVLenghtInCluster=distFromT/5*6
        #maxLVLenghtInCluster=120
        try:
            netID=tree.getNetID(centers.values()[0])
        except:
            netID=0
            tree._nodesByNetID[0]=[]
            tree._network[netID]=[]
        start=time.time()
        for ID in centers.keys():
            nodesByNodeID={}
            segments,lvCost=CMST_dfs.CMST(nodesByClusterID[ID],maxLVLenghtInCluster,centers[ID])
           
            for segment in segments.values():
                node1=segment.getNode1()
                node2=segment.getNode2()
                if not nodesByNodeID.has_key(node1.getID()):
                    nodesByNodeID[node1.getID()]=node1

                if not nodesByNodeID.has_key(node2.getID()):
                    nodesByNodeID[node2.getID()]=node2

            for node in nodesByNodeID.values():                 
                tree._netIDByNode[node] = netID
                tree._nodesByNetID[netID].append(node)

            for segment in segments.values():
                tree._network[netID].append(segment)

        
        finish=time.time()
    
        #Tree=addLVSeg(tree,centers,nodesByClusterID) # Can be used when dedicated lines between Transformer and HHs.
        LVLength=tree.getTotalEdgeWeight()-MVLength
        print "LVLength_check", LVLength
        LVCost=LVLength*LV
        print "MVLength", MVLength
        print "Num Transformers", numTransformer
        print "Total LV Cost", LVCost
        print "Total MV Cost", MVCost
        transformerCost=numTransformer*TCost
        print "Transformer Cost",transformerCost
        print "Total Cost=" ,MVCost+LVCost+transformerCost
        
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp")
        
        print "Total Running Time:",time.time()-startTime
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)

        #if len(args) == 0:
            #raise Usage("No arguments specified.")
        try:
            #inputShapeFile = argv[1]
            #inputShapeFile=r"home/selin/Desktop/windows-desktop/TRANSFORMER/Tiby_Roofs/TibyQB2005Pts.shp"
            inputShapeFile=r"/home/selin/Desktop/TibyParts/T3.shp"
            #inputShapeFile=r"home/selin/Desktop/windows-desktop/TRANSFORMER/Tiby_Roofs/TibyQB2005Pts"
            #outputDir = argv[2]
            outputDir=r"/home/selin/Desktop/TibyParts/T3"
            #algorithm = argv[3]
            searchRadius = "1000 meters"
        except IndexError:
            raise Error("Not enough arguments provided to script.")
	print "T3"
        startTime = time.time()
        print "Generating Dictionaries"
       
        nodesByClusterID,clusterByNode,nodes,centers,LVCostDict=generateDictsFromShp(inputShapeFile,outputDir)
       
        print "Run function starts..."
        timeBeforeRun=time.time()
        totalCost,tree,centers,nodesByClusterID,LVCostDict=run(centers,nodesByClusterID,clusterByNode,LVCostDict,searchRadius)
        print "Time for RUN:", time.time()-timeBeforeRun
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp")
        
        statsFile= outputDir + os.sep + "LVCostDict.txt" 
        writeLVDictToText(statsFile, LVCostDict)
        #batchPrimsforTransformers.batchPrims(tree,centers,LVCostDict,outputDir)
       
        print "LVCostDict=", sum(LVCostDict.values())
        MVLength=tree.getTotalEdgeWeight()
        MVCost=MVLength*25
        print "Total MV Cost", MVCost
        afterrun=time.clock()
        '''Use below if multipoint LV lines are needed'''
        try:
            netID=tree.getNetID(centers.values()[0])
        except:
            netID=0
            tree._nodesByNetID[0]=[]
            tree._network[netID]=[]
        start=time.time()
        for ID in centers.keys():
            nodesByNodeID={}
            segments,lvCost,households_CMST=CMST_dfs.CMST(nodesByClusterID[ID],600,centers[ID])
            #print "Nodes en son", households_CMST
            for segment in segments.values():
                node1=segment.getNode1()
                node2=segment.getNode2()
                if not nodesByNodeID.has_key(node1.getID()):
                    nodesByNodeID[node1.getID()]=node1


                if not nodesByNodeID.has_key(node2.getID()):
                    nodesByNodeID[node2.getID()]=node2

            for node in nodesByNodeID.values():                 
                tree._netIDByNode[node] = netID
                tree._nodesByNetID[netID].append(node)

            for segment in segments.values():
                tree._network[netID].append(segment)


        finish=time.time()
        timeCMST=start-finish
        print "Time CMST", timeCMST
        #Tree=addLVSeg(tree,centers,nodesByClusterID)
        LVLength=tree.getTotalEdgeWeight()-MVLength
        LVCost=LVLength*10
        print "Total LV Cost", LVCost
        transformerCost=len(centers)*5000
        print "Transformer Cost",transformerCost
        print "Total Cost=" ,MVCost+LVCost+transformerCost
        
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp")
        
        print "Total Running Time:",time.time()-startTime
コード例 #11
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        if len(args) == 0:
            raise Usage("No arguments specified.")

        inputShapefile = argv[1]
        startPtShapefile = argv[2]
        outputShapefile = argv[3]
        nodeShapefile = argv[4]
        nodeShapefile1 = argv[5]
        lengthTable = argv[6]
        maxNodes = int(argv[7])

        gp.addMessage("Reading input nodes...")
        # Read nodes from input shapefile, creating network.Node instances.
        rows = gp.searchCursor(inputShapefile)
        row = rows.next()
        desc = gp.describe(inputShapefile)
        nodes = {}
        while row:
            feat = row.getValue(desc.ShapeFieldName)
            pnt = feat.getpart()
            # Read nodes.
            try:
                ptWeight = row.getValue("Weight")
            except:
                ptWeight = 1
            ptFID = row.getValue("FID")
            nodes[ptFID] = network.Node(ptFID, pnt.x, pnt.y, ptWeight)
            row = rows.next()
        del rows # ensure cursor closes

        outputPath = os.path.dirname(outputShapefile)
        if not os.path.exists(outputPath):
            os.mkdir(outputPath)
        distFile = outputPath + os.sep + "dists.txt"
        nodeFile = outputPath + os.sep + "nodes.txt"

        # Copy projection information to shpProj.prj file for later use
        rootDir, fc = os.path.split(inputShapefile)
        base, ext =  os.path.splitext(fc)
        projFile = rootDir + os.sep + base + ".prj"
        newProjFile = outputPath + os.sep + "shpProj.prj"
        try:
            if os.path.exists(newProjFile):
                os.remove(newProjFile)
            shutil.copy(projFile, newProjFile)
            projFile = newProjFile
        except WindowsError:
            gp.AddError("Could not find projection file")

        # Read starting point coordinates from the point in startPtShapefile
        rows = gp.searchCursor(startPtShapefile)
        desc = gp.describe(startPtShapefile)
        row = rows.next()
        feat = row.GetValue(desc.ShapeFieldName)
        FID = row.getValue("FID")
        point = feat.getPart(0)
        startNode = network.Node(FID, point.x, point.y, None)
        del rows
        
        firstNodeFID = nearestNode(startNode, nodes)

        gp.addMessage("Calculating tree...")
        tree = primsAlg(nodes, maxNodes, firstNodeFID)

        gp.addMessage("Writing output...")
        if os.path.exists(outputShapefile):
            gp.Delete_management(outputShapefile)
        fileRW.genShapefile(tree, projFile, outputShapefile)
        
        # Output a shapefile of the nodes in the network.
        if os.path.exists(nodeShapefile):
            gp.Delete_management(nodeShapefile)
        rootDir, fc = os.path.split(nodeShapefile)
        gp.CreateFeatureclass_management(rootDir, fc, "POINT")
        outDesc = gp.describe(nodeShapefile)
        shapefield = outDesc.ShapeFieldName
        origFIDField = "OrigFID"
        gp.addfield(nodeShapefile, origFIDField, "LONG") 
        lengthField = "Length"
        gp.addfield(nodeShapefile, lengthField, "FLOAT") 
        gp.deleteField_management(nodeShapefile,"Id")
        nodeRows = gp.insertCursor(nodeShapefile)
        point = gp.createobject("Point")
        for node in tree.getNodes():
            nodeFID = node.getID()
            sql = '"FID"=%i' %(nodeFID)
            rows = gp.searchCursor(inputShapefile, sql)
            oldRow = rows.next()
            row = nodeRows.newRow()
            row.setValue(origFIDField, nodeFID)
            row.setValue(lengthField, oldRow.getValue(lengthField))
            point.x = node.getX()
            point.y = node.getY()
            row.SetValue(shapefield, point)
            nodeRows.insertRow(row)
        del nodeRows
        del rows
        gp.defineprojection_management(nodeShapefile, projFile)
        if os.path.exists(nodeShapefile1):
            gp.Delete_management(nodeShapefile1)
        gp.CopyFeatures_management(nodeShapefile, nodeShapefile1)

        # Return a DBF with one field, the total length of the network.
        if os.path.exists(lengthTable):
            gp.Delete_management(lengthTable)
        rootDir, table = os.path.split(lengthTable)
        gp.CreateTable_management(rootDir, table)
        totalLenField = "totalLen"
        gp.addfield(lengthTable, totalLenField, "FLOAT") 
        gp.deleteField(lengthTable, "Field1")
        rows = gp.insertCursor(lengthTable)
        row = rows.newRow()
        row.setValue(totalLenField, tree.getTotalEdgeWeight())
        rows.insertRow(row)
        del rows
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        try:
            #inputShapeFile = argv[1]
            
            inputShapeFile=r"/home/selin/Desktop/windows-desktop/real/Ikaram/IkaramPts.shp"
            
            #outputDir = argv[2]
            outputDir=r"/home/selin/Desktop/windows-desktop/real/Ikaram/IkaramPts"
            SR=100000000
            coverDist = 500
            maxLVLenghtInCluster=600
        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()

        clusters=collections.defaultdict(list)
        print "Generating Dictionaries"
        nodes=generateNodeDictFromShp(inputShapeFile,outputDir)
       
        nodesByClusterID,clustersByNodeID=generateClusterDicts(nodes,coverDist)
        
        statsFile= outputDir + os.sep + "Centers_SetCover.csv"
        csvWriter = csv.writer(open(statsFile, 'wb'))
        outFile = open(statsFile,"w")
        centers={}
        while nodesByClusterID:
            maxID=findTheBiggestCluster(nodesByClusterID)
            centers[maxID]=nodes[maxID]
            #centers[maxID]=generateRootNode(nodesByClusterID, maxID,nodes) #eger center of mass istersem
            size=len(nodesByClusterID[maxID])
            nodesByClusterID,clusters=updateDicts(nodesByClusterID,maxID,clusters,nodes)
            
            x=nodes[maxID].getX()
            y=nodes[maxID].getY()
            csvWriter.writerow([x, y, size])
        outFile.close()
        print "Num Clusters", len(clusters)
        print "Total Running Time:",time.time()-startTime

        '''
        try:
            netID=tree.getNetID(centers.values()[0])
        except:
            netID=0
            tree._nodesByNetID[0]=[]
            tree._network[netID]=[]
        '''


        
        
        #roots={}
        #roots=generateRootNode(nodesByClusterID)
        
        tree=network.Network()

        print "Generating LV network"
        
        netID=0
        tree._nodesByNetID[0]=[]
        tree._network[netID]=[]

       
            
        
        for ID in clusters.keys():
            nodesByNodeID={}
            segments,lvCost=CMST_dfs.CMST(clusters[ID],maxLVLenghtInCluster,centers[ID])
           
            for segment in segments.values():
                node1=segment.getNode1()
                node2=segment.getNode2()
                if not nodesByNodeID.has_key(node1.getID()):
                    nodesByNodeID[node1.getID()]=node1

                if not nodesByNodeID.has_key(node2.getID()):
                    nodesByNodeID[node2.getID()]=node2

            for node in nodesByNodeID.values():                 
                tree._netIDByNode[node] = netID
                tree._nodesByNetID[netID].append(node)

            for segment in segments.values():
                tree._network[netID].append(segment)

        print "Generating MV network"
        

        LVLen=tree.getTotalEdgeWeight()
        print "LVLen", LVLen
        segments=generateSegments(centers,SR)
        
        MVtree,segments=kruskalsAlg(segments,centers)
        MVLen=MVtree.getTotalEdgeWeight()
        print "MVLen", MVLen
        fileRW.genShapefile(MVtree, outputDir + ".prj", outputDir + os.sep + "MV.shp")
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "LV.shp")
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)
        try:
            #inputShapeFile = argv[1]

            inputShapeFile = r"/home/selin/Desktop/windows-desktop/real/Ikaram/IkaramPts.shp"

            #outputDir = argv[2]
            outputDir = r"/home/selin/Desktop/windows-desktop/real/Ikaram/IkaramPts"
            SR = 100000000
            coverDist = 500
            maxLVLenghtInCluster = 600
        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()

        clusters = collections.defaultdict(list)
        print "Generating Dictionaries"
        nodes = generateNodeDictFromShp(inputShapeFile, outputDir)

        nodesByClusterID, clustersByNodeID = generateClusterDicts(
            nodes, coverDist)

        statsFile = outputDir + os.sep + "Centers_SetCover.csv"
        csvWriter = csv.writer(open(statsFile, 'wb'))
        outFile = open(statsFile, "w")
        centers = {}
        while nodesByClusterID:
            maxID = findTheBiggestCluster(nodesByClusterID)
            centers[maxID] = nodes[maxID]
            #centers[maxID]=generateRootNode(nodesByClusterID, maxID,nodes) #eger center of mass istersem
            size = len(nodesByClusterID[maxID])
            nodesByClusterID, clusters = updateDicts(nodesByClusterID, maxID,
                                                     clusters, nodes)

            x = nodes[maxID].getX()
            y = nodes[maxID].getY()
            csvWriter.writerow([x, y, size])
        outFile.close()
        print "Num Clusters", len(clusters)
        print "Total Running Time:", time.time() - startTime
        '''
        try:
            netID=tree.getNetID(centers.values()[0])
        except:
            netID=0
            tree._nodesByNetID[0]=[]
            tree._network[netID]=[]
        '''

        #roots={}
        #roots=generateRootNode(nodesByClusterID)

        tree = network.Network()

        print "Generating LV network"

        netID = 0
        tree._nodesByNetID[0] = []
        tree._network[netID] = []

        for ID in clusters.keys():
            nodesByNodeID = {}
            segments, lvCost = CMST_dfs.CMST(clusters[ID],
                                             maxLVLenghtInCluster, centers[ID])

            for segment in segments.values():
                node1 = segment.getNode1()
                node2 = segment.getNode2()
                if not nodesByNodeID.has_key(node1.getID()):
                    nodesByNodeID[node1.getID()] = node1

                if not nodesByNodeID.has_key(node2.getID()):
                    nodesByNodeID[node2.getID()] = node2

            for node in nodesByNodeID.values():
                tree._netIDByNode[node] = netID
                tree._nodesByNetID[netID].append(node)

            for segment in segments.values():
                tree._network[netID].append(segment)

        print "Generating MV network"

        LVLen = tree.getTotalEdgeWeight()
        print "LVLen", LVLen
        segments = generateSegments(centers, SR)

        MVtree, segments = kruskalsAlg(segments, centers)
        MVLen = MVtree.getTotalEdgeWeight()
        print "MVLen", MVLen
        fileRW.genShapefile(MVtree, outputDir + ".prj",
                            outputDir + os.sep + "MV.shp")
        fileRW.genShapefile(tree, outputDir + ".prj",
                            outputDir + os.sep + "LV.shp")
コード例 #14
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help"])
        except getopt.GetoptError, msg:
            raise Usage(msg)

        #if len(args) == 0:
            #raise Usage("No arguments specified.")
        try:
            #inputShapeFile = argv[1]
            #inputShapeFile=r"C:\Documents and Settings\Selin\Desktop\Mbola\MbolaPts.shp"
            inputShapeFile=r"/home/selin/Desktop/uniform/uniform.shp"
            #inputShapeFile=r"C:\Documents and Settings\Selin\Desktop\sampleDir\Pampaida\PampaidaPts.shp"
            #outputDir = argv[2]
            outputDir=r"/home/selin/Desktop/uniform/uniform"
            #algorithm = argv[3]
            searchRadius = 2500
        except IndexError:
            raise Error("Not enough arguments provided to script.")

        startTime = time.time()
        print "Generating Dictionaries"
       
        nodesByClusterID,clusterByNode,nodes,centers,LVCostDict=generateDictsFromShp(inputShapeFile,outputDir)
       
        print "Run function starts..."
        timeBeforeRun=time.time()
        totalCost,tree,centers,nodesByClusterID,LVCostDict=run(centers,nodesByClusterID,clusterByNode,LVCostDict,searchRadius)
        print "Time for RUN:", time.time()-timeBeforeRun
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "MV.shp")
        
        statsFile= outputDir + os.sep + "LVCostDict.txt" 
        writeLVDictToText(statsFile, LVCostDict)
        #batchPrimsforTransformers.batchPrims(tree,centers,LVCostDict,outputDir)
       
        print "LVCostDict=", sum(LVCostDict.values())
        MVLength=tree.getTotalEdgeWeight()
        MVCost=MVLength*25
        print "Total MV Cost", MVCost
        afterrun=time.clock()
        '''Use below if multipoint LV lines are needed'''
        try:
            netID=tree.getNetID(centers.values()[0])
        except:
            netID=0
            tree._nodesByNetID[0]=[]
            tree._network[netID]=[]
        start=time.time()
        for ID in centers.keys():
            nodesByNodeID={}
            segments,lvCost,households_CMST=CMST_dfs.CMST(nodesByClusterID[ID],600,centers[ID])
            #print "Nodes en son", households_CMST
            for segment in segments.values():
                node1=segment.getNode1()
                node2=segment.getNode2()
                if not nodesByNodeID.has_key(node1.getID()):
                    nodesByNodeID[node1.getID()]=node1


                if not nodesByNodeID.has_key(node2.getID()):
                    nodesByNodeID[node2.getID()]=node2

            for node in nodesByNodeID.values():                 
                tree._netIDByNode[node] = netID
                tree._nodesByNetID[netID].append(node)

            for segment in segments.values():
                tree._network[netID].append(segment)


        finish=time.time()
        timeCMST=start-finish
        print "Time CMST", timeCMST
        #Tree=addLVSeg(tree,centers,nodesByClusterID)
        LVLength=tree.getTotalEdgeWeight()-MVLength
        LVCost=LVLength*10
        print "Total LV Cost", LVCost
        transformerCost=len(centers)*5000
        print "Transformer Cost",transformerCost
        print "Total Cost=" ,MVCost+LVCost+transformerCost
        
        fileRW.genShapefile(tree, outputDir + ".prj", outputDir + os.sep + "FinalGrid.shp")
        
        print "Total Running Time:",time.time()-startTime