def inc(self, cy): """ Returns True if self includes cy """ npoints = filter(lambda x: x < 0, self.cycle) coords = map(lambda x: self.G.pos[x], npoints) polyself = geu.Polygon(sh.MultiPoint(tuple(coords)), self.cycle) npoints = filter(lambda x: x < 0, cy.cycle) coords = map(lambda x: cy.G.pos[x], npoints) polycy = geu.Polygon(sh.MultiPoint(tuple(coords)), cy.cycle) pdb.set_trace() bool = polyself.intersect(polycy) return (bool)
def test_ptconvex2(self): print("testing geomutil.ptconvex2") points = shg.MultiPoint([(0, 0), (0, 1), (3.2, 1), (3.2, 0.7), (0.4, 0.7), (0.4, 0)]) polyg = geu.Polygon(points) cvex, ccave = polyg.ptconvex2() assert_equal(cvex, [-5]) assert_equal(ccave, [-1, -2, -3, -4, -6]) points = shg.MultiPoint([(0, 0), (0, 1), (-3.2, 1), (-3.2, 0.7), (-0.4, 0.7), (-0.4, 0)]) polyg = geu.Polygon(points) cvex, ccave = polyg.ptconvex2() assert_equal(cvex, [-5]) assert_equal(ccave, [-1, -2, -3, -4, -6])
def __init__(self, refs, tags, coords): """ object constructor Parameters ---------- refs : tags : coords : nodes_sign : int if data comes from osm nodes are >0 in ways sequence if data comes from josm editor nodes are <0 ways sequence """ self.refs = refs self.tags = tags N = len(refs) p = np.zeros((2, N)) self.valid = True for k, nid in enumerate(refs): try: p[0, k] = coords.xy[nid][0] p[1, k] = coords.xy[nid][1] except: self.valid = False break # closed way or open way if self.valid: if (N >= 4) & (refs[0] == refs[-1]): self.shp = geu.Polygon(p) self.typ = 0 else: self.shp = geu.LineString(p) self.typ = 1
def show(self, **kwargs): """ show cycle acceleration can be obtained if the polygon is calculated once """ #nx.draw_networkx_edges(self.G,self.G.pos,width=2,edge_color=color,alpha=0.4) npoints = filter(lambda x: x < 0, self.cycle) coords = map(lambda x: self.G.pos[x], npoints) poly = geu.Polygon(sh.MultiPoint(tuple(coords)), self.cycle) fig, ax = poly.plot(**kwargs) return fig, ax
def vrml2sha(tg): """ convert vrml object into shapely polygons Parameters ---------- tg : list of objects """ for l in tg: for k in l.keys(): if k != 'name': c = l[k]['coord'] i = l[k]['index'] tt = [] ltt = [] for u in i: if u == -1: # -1 closure indicator ltt.append(tt) tt = [] else: tt.append(u) P = geo.Polygon(c)
def load(self, filename): """ Parameters ---------- filename : str """ dg = parsevrml(filename) self.entity = {} for t in dg: # WALL, COLUMN, DOOR , STAIR , SPACE self.entity[t] = {} k = 0 for ID in dg[t]: c = dg[t][ID]['coord'] self.entity[t][k] = {} self.entity[t][k]['ID'] = ID self.entity[t][k]['coord'] = c l = dg[t][ID]['index'] dp = {} p = [] kk = 0 for il in l: if il == -1: dp[kk] = p p = [] kk = kk + 1 else: p.append(il) self.entity[t][k]['index'] = dp k = k + 1 # # Simplify Coord (Projection in 0xy plane) # for g in self.entity: for ID in self.entity[g]: x = self.entity[g][ID]['coord'][:, 0] y = -self.entity[g][ID]['coord'][:, 2] z = self.entity[g][ID]['coord'][:, 1] tp = np.vstack((x, y)) Np = np.shape(tp)[1] tp2 = {} already = False iop = 0 for ip in range(Np): p = tp[:, ip] for iold in tp2.keys(): pold = tp2[iold]['coord'] if np.shape(pold) == (2, ): dist = np.dot(p - pold, p - pold) if dist < 1e-15: already = True tp2[iold]['nump'].append(ip) if not already: tp2[iop] = {} tp2[iop]['coord'] = p tp2[iop]['nump'] = [ip] iop = iop + 1 already = False self.entity[g][ID]['c2d'] = tp2 # # create a transcode between 3d index point and 2d index point # for g in self.entity: for ID in self.entity[g]: tr = {} dr2 = self.entity[g][ID]['c2d'] for k in dr2.keys(): for u in dr2[k]['nump']: tr[u] = k self.entity[g][ID]['tr'] = tr # # Create a new index for 2d points # for g in self.entity: for ID in self.entity[g]: di = self.entity[g][ID]['index'] di2 = {} for k in di.keys(): # for all polygons ti2 = [] # reserve a list= for 2d indexes lpoly = di[k] # get sequence of 3d points for ip in lpoly: ti2.append(self.entity[g][ID]['tr'][ip]) if len(np.unique(ti2)) == len(ti2): di2[k] = ti2 self.entity[g][ID]['index2'] = di2 # # Create Polygon2D # for g in self.entity: for ID in self.entity[g]: dp = {} for ip in self.entity[g][ID]['index2']: lp = self.entity[g][ID]['index2'][ip] tp = [] for ip in lp: tp.append(self.entity[g][ID]['c2d'][ip]['coord']) poly = geo.Polygon(tp) dp[ip] = poly self.entity[g][ID]['poly'] = dp
def getosm(address='Rennes', latlon=0, dist_m=400, cart=False): """ get osm region from osmapi Parameters ---------- address : string latlon : tuple or 0 dist_m : float cart : boolean Notes ----- if latlon tuple is precised it has priority over the string """ level_height = 3.45 rad_to_deg = (180 / np.pi) deg_to_rad = (np.pi / 180) if latlon == 0: place = geo.google(address) try: lat, lon = place.latlng except: print place else: lat = latlon[0] lon = latlon[1] r_earth = 6370e3 alpha = (dist_m / r_earth) * rad_to_deg Osm = OsmApi() # # Get Map around the specified coordinates # osmmap = Osm.Map(lon - alpha, lat - alpha, lon + alpha, lat + alpha) #print(osmmap) nodes = Nodes() nodes.clean() nodes.readmap(osmmap) coords = Coords() coords.clean() coords.from_nodes(nodes) m = coords.cartesian(cart=cart) ways = Ways() ways.clean() ways.readmap(osmmap, coords) # list of nodes involved in buildings lnodes_id = [] for iw in ways.w: lnodes_id += ways.w[iw][0] # list of all nodes of coords lnodes_id = np.unique(np.array(lnodes_id)) lnodes_full = np.unique(np.array(coords.latlon.keys())) mask = np.in1d(lnodes_full, lnodes_id, invert=True) # nodes not involved in buildings lexcluded = lnodes_full[mask] coords.filter(lexcluded) dpoly = {} for iw in ways.w: ways.way[iw].tags = {} # material if ways.w[iw][1].has_key('material'): ways.way[iw].tags['name'] = ways.w[iw][1]['material'] elif ways.w[iw][1].has_key('building:material'): ways.way[iw].tags['name'] = ways.w[iw][1]['building:material'] else: ways.way[iw].tags['name'] = 'WALL' # height if ways.w[iw][1].has_key('height'): ways.way[iw].tags['z'] = (0, eval(ways.w[iw][1]['height'])) elif ways.w[iw][1].has_key('building:height'): ways.way[iw].tags['z'] = (0, eval(ways.w[iw][1]['building:height'])) elif ways.w[iw][1].has_key('building:levels'): nb_levels = eval(ways.w[iw][1]['building:levels']) if type(nb_levels) != int: try: nb_levels = max(nb_levels) except: nb_levels = 2 ways.way[iw].tags['z'] = (0, nb_levels * level_height) elif ways.w[iw][1].has_key('levels'): nb_levels = eval(ways.w[iw][1]['levels']) if type(nb_levels) != int: try: nb_levels = max(nb_levels) except: nb_levels = 2 ways.way[iw].tags['z'] = (0, nb_levels * level_height) else: ways.way[iw].tags['z'] = (0, 12) ptpoly = [coords.xy[x] for x in ways.w[iw][0]] dpoly[iw] = geu.Polygon(ptpoly, vnodes=ways.w[iw][0]) dpoly[iw].coorddeter() return coords, nodes, ways, dpoly, m
def inclusion(self, Cy): """ Return True if self includes Cy Parameters ---------- Cy : Cycle Returns ------- boolean : True if Cy \subset self path : path Notes ----- Inclusion implies : 1. The area of the including cycle is larger than the area of included cycle 2. The 2 cycles have at least a common segment 3. When the common segment is travelled with the same orientation both cycle SignedArea have the same sign """ if abs(self.area) > abs(Cy.area): flip, path = self.intersect(Cy) if len(path) > 0: if path[0] > 0: # cycles share a segments s1 = Cy.area * self.area if flip: # sens oppose if s1 > 0: return False, path else: return True, path else: # meme sens if s1 > 0: return True, path else: return False, path else: #cycles share a point # signed area is not sufficent to determien if # 2 polygon connect by a single point # are inclusive or external # areabig = abs(self.area) # #areasmall = abs(Cy.area) # cycomb = self + Cy # areacomb = abs(cycomb.area) # if areacomb < areabig: # return True,path # else: # return False,np.array([]) PCy = geu.Polygon(p=Cy.p, vnodes=Cy.cycle) Pself = geu.Polygon(p=self.p, vnodes=self.cycle) inter = PCy.intersection(Pself) # if intesection is a point, Cy not subset of self if isinstance(inter, sh.Point): return False, np.array([]) else: return True, path else: return False, np.array([]) else: return False, np.array([])