def checkExtent(self, metaExtent, checkExtent, operation): """ @summary: checks polygons against operation @param metaExtent: list in the format [minx,miny,maxx,maxy] @param checkExtent: list in the format [minx,miny,maxx,maxy] """ metaExtent = [float(x) for x in metaExtent] checkExtent = [float(x) for x in checkExtent] metaPoly = Polygon( ((metaExtent[0], metaExtent[1]), (metaExtent[0], metaExtent[3]), (metaExtent[2], metaExtent[3]), (metaExtent[2], metaExtent[1]))) checkPoly = Polygon( ((checkExtent[0], checkExtent[1]), (checkExtent[0], checkExtent[3]), (checkExtent[2], checkExtent[3]), (checkExtent[2], checkExtent[1]))) if operation == "Contains": return checkPoly.contains(metaPoly) if operation == "Intersects": return checkPoly.intersects(metaPoly) if operation == "Equals": return checkPoly.equals(metaPoly) if operation == "Touches": return checkPoly.touches(metaPoly) if operation == "Within": return checkPoly.within(metaPoly) if operation == "Outside": return checkPoly.disjoint(metaPoly)
def compute_intersection_demo(): """ calculating area of polygon inside region :return: """ print("compute_intersection_demo") # http://toblerity.org/shapely/manual.html a = Point(1, 1).buffer(1.5) b = Point(2, 1).buffer(1.5) c = a.intersection(b) print("intersect area (circles): ", c.area) # http://toblerity.org/shapely/manual.html#polygons a = Polygon([(0, 0), (1, 1), (1, 0)]) b = Polygon([(0.5, 0.5), (1.5, 1.5), (1.5, 0.5)]) c = a.intersection(b) print("intersect area (polygons): ", c.area) # poly = Polygon(list(zip(X[0], Y[0]))) # a = box(0, 0, 3, 3) # patch b = Polygon([(0, 0), (0, 2), (2, 2), (2, 0)]) # roi c = Polygon([(0.5, 0.5), (1.0, 1.0), (1.5, 0.5), (1.0, 0.0)]) # polygon d = c.intersection(b) print("poly area", c.area) print("intersect area (poly, roi): ", d.area) # WITHIN: # object's boundary and interior intersect only with the interior of the other # (not its boundary or exterior) print("poly within roi: ", c.within(b)) # CROSSES: # interior of the object intersects the interior of the other but does not contain it... print("poly crosses roi: ", c.crosses(b)) # DISJOINT: boundary and interior of the object do not intersect at all print("poly does not intersect roi: ", c.disjoint(b))
def plateau_to_building(data): if "building_limits" not in data: raise ValueError("Building limits does not exist.") if "height_plateaus" not in data: raise ValueError("Height plateaus does not exist.") building_coors = data["building_limits"]["features"][0]["geometry"][ "coordinates"][0] building_obj = Polygon(building_coors) if "building_and_height" in data: return "Building limits with heights already exist" else: data["building_and_height"] = { "type": "FeatureCollection", "features": [] } total_area = 0.0 plateau_intersec = Polygon( data["height_plateaus"]["features"][0]["geometry"]["coordinates"][0]) for i in range(len(data["height_plateaus"]["features"])): plateau_coors = data["height_plateaus"]["features"][i]["geometry"][ "coordinates"][0] elevation = data["height_plateaus"]["features"][i]["properties"][ "elevation"] plateau_obj = Polygon(plateau_coors) if i > 0: if plateau_intersec.crosses(plateau_obj): raise ValueError( "There is an overlapping between height plateaus coordinates" ) if plateau_intersec.disjoint(plateau_obj): raise ValueError( "There is a gap between height plateaus coordinates") total_area += plateau_obj.area plateau_intersec = plateau_intersec.union(plateau_obj) building_plateau = list( building_obj.intersection(plateau_obj).exterior.coords) data["building_and_height"]["features"].append({ "geometry": { "coordinates": [building_plateau], "type": "Polygon" }, "properties": { "elevation": elevation }, "type": "Feature" }) if not plateau_intersec.contains(building_obj): raise ValueError("Height plateaus does not cover building limits") with portalocker.Lock(OUTPUT_JSON_PATH, 'w+') as f: json.dump(data, f, indent=4) return "Success. Json file saved in {}".format(OUTPUT_PATH)
def _calculate_position_between_objects(self, boxA: BoundBox, boxB: BoundBox): polygon = Polygon(self.convert_top_bottom_to_polygon(boxA)) other_polygon = Polygon(self.convert_top_bottom_to_polygon(boxB)) vsName = boxA.label + ' vs ' + boxB.label positions = {vsName: {}} # positions[vsName] if polygon.crosses(other_polygon): positions[vsName]['crosses'] = polygon.crosses(other_polygon) if polygon.contains(other_polygon): positions[vsName]['contains'] = polygon.contains(other_polygon) if polygon.disjoint(other_polygon): positions[vsName]['disjoint'] = polygon.disjoint(other_polygon) if polygon.intersects(other_polygon): positions[vsName]['intersects'] = polygon.intersects(other_polygon) if polygon.overlaps(other_polygon): positions[vsName]['overlaps'] = polygon.overlaps(other_polygon) if polygon.touches(other_polygon): positions[vsName]['touches'] = polygon.touches(other_polygon) if polygon.within(other_polygon): positions[vsName]['within'] = polygon.within(other_polygon) return positions
def test_prepared_predicates(): # check prepared predicates give the same result as regular predicates polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]) polygon2 = Polygon([(0.5, 0.5), (1.5, 0.5), (1.0, 1.0), (0.5, 0.5)]) point2 = Point(0.5, 0.5) polygon_empty = Polygon() prepared_polygon1 = PreparedGeometry(polygon1) for geom2 in (polygon2, point2, polygon_empty): assert polygon1.disjoint(geom2) == prepared_polygon1.disjoint(geom2) assert polygon1.touches(geom2) == prepared_polygon1.touches(geom2) assert polygon1.intersects(geom2) == prepared_polygon1.intersects( geom2) assert polygon1.crosses(geom2) == prepared_polygon1.crosses(geom2) assert polygon1.within(geom2) == prepared_polygon1.within(geom2) assert polygon1.contains(geom2) == prepared_polygon1.contains(geom2) assert polygon1.overlaps(geom2) == prepared_polygon1.overlaps(geom2)
def _pastePolyToPoly(self, bgMk: List[Dict], fgMk: Dict, frSize: Tuple) -> List[Dict]: """ paste foreground polygon on background polygon at `pos` position Parameter --------- frSize frame size of the image """ fgPol = Polygon(fgMk['param']) # foreground polygon frPol = box(0, 0, frSize[0], frSize[1]) # image frame polygon resMk = [] # ignore any foreground that is out side of image frame or # background makring that is empty ## NOTE: a.difference(b) is slower compare to binary predicates if not (frPol.disjoint(fgPol) or frPol.touches(fgPol) or bgMk is None): if frPol.overlaps(fgPol): # partial of foreground is outside of frame fgPol = frPol.intersection(fgPol) for mk in bgMk: bgPol = Polygon(mk['param']) obj_cp = None # object that needs copy if fgPol.disjoint(bgPol) or fgPol.touches(bgPol): # completely isolate or outer_cut obj_cp = bgPol elif fgPol.contains(bgPol): # foreground cover background completely obj_cp = fgPol else: obj_cp = bgPol.difference(fgPol) if isinstance(obj_cp, Polygon): obj_cp = [obj_cp] for geo in obj_cp: param = numpy.array(geo.exterior.coords) if len(geo.interiors) > 0: param = numpy.append([param], geo.interiors) resMk.append({'param': param, 'type': 'polygon'}) resMk.append({'param': fgMk['param'], 'type': 'polygon'}) elif bgMk is not None: return copy.deepcopy(bgMk) return resMk
def test_prepared_predicates(): # check prepared predicates give the same result as regular predicates polygon1 = Polygon([ (0, 0), (0, 1), (1, 1), (1, 0), (0, 0) ]) polygon2 = Polygon([ (0.5, 0.5), (1.5, 0.5), (1.0, 1.0), (0.5, 0.5) ]) point2 = Point(0.5, 0.5) polygon_empty = Polygon() prepared_polygon1 = PreparedGeometry(polygon1) for geom2 in (polygon2, point2, polygon_empty): assert polygon1.disjoint(geom2) == prepared_polygon1.disjoint(geom2) assert polygon1.touches(geom2) == prepared_polygon1.touches(geom2) assert polygon1.intersects(geom2) == prepared_polygon1.intersects(geom2) assert polygon1.crosses(geom2) == prepared_polygon1.crosses(geom2) assert polygon1.within(geom2) == prepared_polygon1.within(geom2) assert polygon1.contains(geom2) == prepared_polygon1.contains(geom2) assert polygon1.overlaps(geom2) == prepared_polygon1.overlaps(geom2)
def cut_map_tiles(src_name, queue, ul, ur, lr, ll, max_zoom): ''' ''' coords = [Coordinate(0, 0, 0)] radius = pi * 6378137 map_bounds = Polygon([(ul.x, ul.y), (ur.x, ur.y), (lr.x, lr.y), (ll.x, ll.y), (ul.x, ul.y)]) while coords: coord = coords.pop(0) xmin, ymin, xmax, ymax = coord2bbox(coord) tile_bounds = Polygon([(xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin), (xmin, ymax)]) if tile_bounds.disjoint(map_bounds): continue tilename = join( dirname(src_name), 'tile-%(zoom)d-%(column)d-%(row)d.tif' % coord.__dict__) cmd = 'gdalwarp -r cubicspline -dstalpha -ts 256 256'.split() cmd += ('-te %(xmin).6f %(ymin).6f %(xmax).6f %(ymax).6f' % locals()).split() cmd += [src_name, tilename] #'-overwrite', #print "%s"%cmd cmd = Popen(cmd, stdout=PIPE, stderr=PIPE) cmd.wait() if cmd.returncode != 0: print cmd.stderr.read() raise Exception('Error in gdalwarp') queue.put((coord, tilename)) if coord.zoom < max_zoom: coords.append(coord.zoomBy(1)) coords.append(coord.zoomBy(1).right()) coords.append(coord.zoomBy(1).right().down()) coords.append(coord.zoomBy(1).down())
def cut_map_tiles(src_name, queue, ul, ur, lr, ll, max_zoom): ''' ''' coords = [Coordinate(0, 0, 0)] radius = pi * 6378137 map_bounds = Polygon([(ul.x, ul.y), (ur.x, ur.y), (lr.x, lr.y), (ll.x, ll.y), (ul.x, ul.y)]) while coords: coord = coords.pop(0) xmin, ymin, xmax, ymax = coord2bbox(coord) tile_bounds = Polygon([(xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin), (xmin, ymax)]) if tile_bounds.disjoint(map_bounds): continue tilename = join(dirname(src_name), 'tile-%(zoom)d-%(column)d-%(row)d.tif' % coord.__dict__) cmd = 'gdalwarp -r cubicspline -dstalpha -ts 256 256'.split() cmd += ('-te %(xmin).6f %(ymin).6f %(xmax).6f %(ymax).6f' % locals()).split() cmd += [src_name, tilename] #'-overwrite', #print "%s"%cmd cmd = Popen(cmd, stdout=PIPE, stderr=PIPE) cmd.wait() if cmd.returncode != 0: print cmd.stderr.read() raise Exception('Error in gdalwarp') queue.put((coord, tilename)) if coord.zoom < max_zoom: coords.append(coord.zoomBy(1)) coords.append(coord.zoomBy(1).right()) coords.append(coord.zoomBy(1).right().down()) coords.append(coord.zoomBy(1).down())
def checkExtent(self, metaExtent, checkExtent, operation): """ @summary: checks polygons against operation @param metaExtent: list in the format [minx,miny,maxx,maxy] @param checkExtent: list in the format [minx,miny,maxx,maxy] """ metaExtent = [float(x) for x in metaExtent] checkExtent = [float(x) for x in checkExtent] metaPoly = Polygon(((metaExtent[0],metaExtent[1]), (metaExtent[0],metaExtent[3]), (metaExtent[2],metaExtent[3]), (metaExtent[2],metaExtent[1]))) checkPoly = Polygon(((checkExtent[0],checkExtent[1]), (checkExtent[0],checkExtent[3]), (checkExtent[2],checkExtent[3]), (checkExtent[2],checkExtent[1]))) if operation == "Contains": return checkPoly.contains(metaPoly) if operation == "Intersects": return checkPoly.intersects(metaPoly) if operation == "Equals": return checkPoly.equals(metaPoly) if operation == "Touches": return checkPoly.touches(metaPoly) if operation == "Within": return checkPoly.within(metaPoly) if operation == "Outside": return checkPoly.disjoint(metaPoly)
def create_polygon(self,points, k=3): print("Current k %s"%k) self.point_list = [] dataset = [] for pnt in points: self.point_list.append(pnt) dataset.append(pnt) self.point_count = len(self.point_list) if self.point_count<3: return None if k>self.point_count: return None firstPoint = self.find_min_y_point(self.point_list) hull = [] hull.append(firstPoint) currentPoint = firstPoint self.point_list.remove(firstPoint) previousPoint = firstPoint step = 2 cutoff = math.floor(float(len(self.point_list))/2) print("Number k: %s"%(k)) while ((currentPoint != firstPoint) or (step==2)) and (len(self.point_list)>0): if step >1000000: print("loop kept going") return None print("Step %s" %(step)) if step == 5: self.point_list.append(firstPoint) kNearestPoints = self.get_nearest_neighbors(self.point_list,currentPoint,k) cpoints = self.sort_by_angle(kNearestPoints,currentPoint,previousPoint) #print len(cpoints) cpoint = None its = True if len(hull) >= 2: for cpoint in cpoints: newEdge = LineString([currentPoint,cpoint]) startHull = 0 if firstPoint.equals(cpoint): print("cpoint equals firstpoint, length of pointList %s"%len(self.point_list)) startHull +=1 crosses = False for i in range(startHull,len(hull),1): #try: if i == len(hull)-1: #print "last check" tempLine =LineString([hull[i],hull[0]]) crosses = newEdge.crosses(tempLine) #print "Crosses back %s"%crosses break tempLine =LineString([hull[i],hull[i+1]]) crosses = newEdge.crosses(tempLine) #print "Crosses %s"%(crosses) if crosses == True: break #if cpoint.disjoint(tempLine) == True: #print "Not on Edge" #else: #print "On Edge" #except: #print "error in crosses check" if crosses == False: its = False break else: its = False cpoint = cpoints[0] if its == True: print("Intersects, probably should increase k") newk=k+1 poly = self.createPolygon(points,newk) return poly previousPoint = currentPoint currentPoint = cpoint hull.append(cpoint) #for i in range(0,hull.count): #print "%s,%s"%(hull[i].X,hull[i].Y) self.point_list.remove(currentPoint) step+=1 #hull.append(firstPoint) hullPolygon = Polygon(sum(map(list, (p.coords for p in hull)), [])) #print ("Part count %s" % list(hullPolygon.interior.coords)) #if len(list(hullPolygon.interior.coords)) > 1: #print("Not all points contained increasing k") #newk=k+1 #if newk > len(points)-1: #return None #poly = self.create_polygon(points,newk) #return poly contains = True #within = True #touches = True disj = False for pnt in dataset: contains = hullPolygon.contains(pnt) #within = pnt.within(hullPolygon) #touches = hullPolygon.touches(pnt) disj = hullPolygon.disjoint(pnt) #print contains if disj==True: print("Not all points contained increasing k") newk=k+1 if newk > len(points)-1: print("Exceeds the number of points") return hullPolygon poly = self.create_polygon(points,newk) return poly return hullPolygon
def preprocessing(grids, window_time,filename): import json ncols = len(grids) index_c = -1 # Index of the current instante T labels = ["instante"] + [i for i in xrange(1,ncols+1)] zeros = np.zeros(ncols).tolist() jam_grids = pd.DataFrame([], columns=labels) # Partial result #West Longitude,South Latitude,East Longitude,North Latitude,IDgrid,Valid WEST = 0 SOUTH = 1 EAST = 2 NORTH = 3 VALID = 5 div_y = grids[0][NORTH] - grids[0][SOUTH] div_x = grids[0][EAST] - grids[0][WEST] init_x = grids[0][WEST] init_y = grids[0][SOUTH] #print "div_y:{} and div_x:{}".format(div_y,div_x) for i, line in enumerate(open(filename,'r')): try: record = json.loads(line) points = record['line'] currentTime = record['pubMillis']["$numberLong"] currentTime = datetime.utcfromtimestamp(float(currentTime)/ 1000.0) currentTime = group_datetime(currentTime, window_time) index_c = jam_grids['instante'].loc[jam_grids['instante'] == str(currentTime)].index.tolist() if index_c == []: row = [ [str(currentTime)] + zeros ] index_c = len(jam_grids) jam_grids = jam_grids.append(pd.DataFrame(row, columns=labels, index=[index_c])) else: index_c = index_c[0] if (i% 10000 == 0): print "Line {} at {}".format(i,currentTime) line = [ (float(pair['y']), float(pair['x'])) for pair in points] shapely_line = LineString(line) #pruning the list of grids bound = shapely_line.bounds miny, minx, maxy, maxx = bound #print "LINE: miny {} and maxy {}".format(miny,maxy) p = abs(miny - init_y) i_min = int(p/div_y)*50 p = abs(maxy - init_y) i_max = int(p/div_y)*50+49 if i_min>=ncols: print "Line #{} - ({},{}]".format(i, i_min,i_max) i_min = ncols-1 if i_max>=ncols: print "Line #{} - ({},{}]".format(i, i_min,i_max) i_max = ncols-1 #print "GRID miny {} and GRID maxy {}".format(grids[i_min][SOUTH],grids[i_max][NORTH]) #print "Checking ({},{}) in {} grids".format(i_min,i_max ,i_max-i_min +1) for col in xrange(i_min, i_max+1): row = grids[col] #0 to 2499 if row[VALID]: polygon = Polygon([ (row[SOUTH], row[WEST]), (row[NORTH], row[WEST]), (row[NORTH], row[EAST]), (row[SOUTH], row[EAST]) ]) shapely_poly = Polygon(polygon) intersection_line = not shapely_poly.disjoint(shapely_line) if intersection_line: jam_grids.ix[index_c, col] += 1 except Exception as e: print "Error at Line #{}. Skipping this line: {}".format(i,line) print e return jam_grids