Beispiel #1
0
def test_is_ccw():
    polya=Polygon(vvector([
        Vertex(0,0),Vertex(1,0),Vertex(1,1),Vertex(0,1)]))
    assert polya.is_ccw()
    polya=Polygon(vvector([
        Vertex(0,0),Vertex(0,1),Vertex(1,1),Vertex(1,0)]))
    assert not polya.is_ccw()
Beispiel #2
0
 def check_area(self,nr2tri):
     if len(self.triangles)==0:
         return
     totarea=0.0
     for trinr in self.triangles:
         tri=nr2tri[trinr]
         coords=[]
         for v in tri.vobjs:
             print "Pos: %s"%((int(v.merc[0]),int(v.merc[1])),)
             coords.append(pyshapemerge2d.Vertex(int(v.merc[0]),int(v.merc[1])))
         poly=Polygon(vvector(coords))
         assert not poly.is_ccw()
         area=-poly.calc_area()
         print "Tri:%s area = %s"%([x.nr for x in tri.vobjs],area)
         totarea+=area
     print "Area should be: %s (since size = %d)"%(self.size*self.size,self.size)
     print "Area is: %s"%(totarea)
     assert abs(totarea-(self.size*self.size))<10.0*10.0
Beispiel #3
0
def cleanup_poly(latlonpoints, name="?"):

    for minstep in [0, 10, 100, 1000, 10000, 100000]:
        mercpoints = []
        lastmerc = None
        for latlon in latlonpoints:
            merc = mapper.latlon2merc(latlon, 13)
            if lastmerc != None:
                dist = math.sqrt(
                    sum([(lastmerc[i] - merc[i])**2 for i in xrange(2)]))
                if dist < minstep:
                    continue
            if merc == lastmerc:
                continue
            lastmerc = merc
            mercpoints.append(Vertex(int(merc[0]), int(merc[1])))
        if len(mercpoints) < 50:
            break
    if len(mercpoints) <= 2: return None
    if mercpoints[0] == mercpoints[-1]:
        del mercpoints[-1]
    if len(mercpoints) <= 2: return None
    poly = Polygon(vvector(mercpoints))
    if len(mercpoints) == 4:
        swapped = [mercpoints[1], mercpoints[0]] + mercpoints[2:]
        swappedpoly = Polygon(vvector(swapped))
        #print "Found 4-corner area: ",name," areas:",swappedpoly.calc_area(),poly.calc_area()
        if abs(swappedpoly.calc_area()) > abs(1.1 * poly.calc_area()):
            print "Untwisting an area", name
            mercpoints = swapped
            poly = swappedpoly

    backtomerc = [
        mapper.merc2latlon((m.get_x(), m.get_y()), 13) for m in mercpoints
    ]
    if poly.is_ccw():
        return backtomerc
    else:
        #print "Reversed "+latlonpoints
        return reversed(backtomerc)
Beispiel #4
0
def get_borders(pcountry):
    #print "Getting for",pcountry
    global borders
    if not borders:
        if not os.path.exists("fplan/extract/lands.bin"):
            if os.system("bunzip2 fplan/extract/lands.bin.bz2")!=0:
                raise Exception("Couldn't unbzip2 lands.bin.bz2")
        f=open("fplan/extract/lands.bin")
        tborders=pickle.load(f)
        f.close()
        out=dict()
        for country,parts in tborders.items():
            outparts=[]
            tot=0
            for part in parts:
                outpart=[]
                poly_coords=[]
                last=None
                for coord in part:
                    merc=mapper.latlon2merc(mapper.from_str(coord),13)
                    if merc==last: continue
                    last=merc
                    outpart.append(merc)
                    tot+=1
                    poly_coords.append(Vertex(int(merc[0]),int(merc[1])))
                assert len(outpart)>=3
                if outpart[0]==outpart[-1]:
                    outpart=outpart[:-1]
                    poly_coords=poly_coords[:-1]
                poly=Polygon(vvector(poly_coords))
                assert poly.is_ccw()

                outparts.append(outpart)
            #print "Parts in ",country,len(outparts),tot
            out[country]=outparts
        borders=out
    #if pcountry!="sweden":
    #    raise Exception("Debug, just allow sweden for now. just remove this after.")
    return borders[pcountry]
Beispiel #5
0
def get_borders(pcountry):
    #print "Getting for",pcountry
    global borders
    if not borders:
        if not os.path.exists("fplan/extract/lands.bin"):
            if os.system("bunzip2 fplan/extract/lands.bin.bz2") != 0:
                raise Exception("Couldn't unbzip2 lands.bin.bz2")
        f = open("fplan/extract/lands.bin")
        tborders = pickle.load(f)
        f.close()
        out = dict()
        for country, parts in tborders.items():
            outparts = []
            tot = 0
            for part in parts:
                outpart = []
                poly_coords = []
                last = None
                for coord in part:
                    merc = mapper.latlon2merc(mapper.from_str(coord), 13)
                    if merc == last: continue
                    last = merc
                    outpart.append(merc)
                    tot += 1
                    poly_coords.append(Vertex(int(merc[0]), int(merc[1])))
                assert len(outpart) >= 3
                if outpart[0] == outpart[-1]:
                    outpart = outpart[:-1]
                    poly_coords = poly_coords[:-1]
                poly = Polygon(vvector(poly_coords))
                assert poly.is_ccw()

                outparts.append(outpart)
            #print "Parts in ",country,len(outparts),tot
            out[country] = outparts
        borders = out
    #if pcountry!="sweden":
    #    raise Exception("Debug, just allow sweden for now. just remove this after.")
    return borders[pcountry]
Beispiel #6
0
def cleanup_poly(latlonpoints,name="?"):
    
    for minstep in [0,10,100,1000,10000,100000]:
        mercpoints=[]
        lastmerc=None
        for latlon in latlonpoints:
            merc=mapper.latlon2merc(latlon,13)
            if lastmerc!=None:
                dist=math.sqrt(sum([(lastmerc[i]-merc[i])**2 for i in xrange(2)]))
                if dist<minstep:
                    continue
            if merc==lastmerc:
                continue
            lastmerc=merc
            mercpoints.append(Vertex(int(merc[0]),int(merc[1])))
        if len(mercpoints)<50:
            break
    if len(mercpoints)<=2: return None
    if mercpoints[0]==mercpoints[-1]:
        del mercpoints[-1]
    if len(mercpoints)<=2: return None
    poly=Polygon(vvector(mercpoints))
    if len(mercpoints)==4:
        swapped=[mercpoints[1],mercpoints[0]]+mercpoints[2:]
        swappedpoly=Polygon(vvector(swapped))
        #print "Found 4-corner area: ",name," areas:",swappedpoly.calc_area(),poly.calc_area()
        if abs(swappedpoly.calc_area())>abs(1.1*poly.calc_area()):
            print "Untwisting an area",name
            mercpoints=swapped
            poly=swappedpoly
            
    backtomerc=[mapper.merc2latlon((m.get_x(),m.get_y()),13) for m in mercpoints]
    if poly.is_ccw():
        return backtomerc
    else:    
        #print "Reversed "+latlonpoints
        return reversed(backtomerc)