def _process_traj(self, traj, max_diameter, min_duration):
        detected_stops = []
        segment_geoms = []
        segment_times = []
        geom = MultiPoint()
        is_stopped = False
        previously_stopped = False

        for index, data in traj.df[traj.get_geom_column_name()].iteritems():
            segment_geoms.append(data)
            geom = geom.union(data)
            segment_times.append(index)

            if not is_stopped:  # remove points to the specified min_duration
                while (
                    len(segment_geoms) > 2
                    and segment_times[-1] - segment_times[0] >= min_duration
                ):
                    segment_geoms.pop(0)
                    segment_times.pop(0)
                # after removing extra points, re-generate geometry
                geom = MultiPoint(segment_geoms)

            if (
                len(segment_geoms) > 1
                and mrr_diagonal(geom, traj.is_latlon) < max_diameter
            ):
                is_stopped = True
            else:
                is_stopped = False

            if len(segment_geoms) > 1:
                segment_end = segment_times[-2]
                segment_begin = segment_times[0]
                if not is_stopped and previously_stopped:
                    if (
                        segment_end - segment_begin >= min_duration
                    ):  # detected end of a stop
                        detected_stops.append(
                            TemporalRangeWithTrajId(segment_begin, segment_end, traj.id)
                        )
                        segment_geoms = []
                        segment_times = []
                        geom = MultiPoint()

            previously_stopped = is_stopped

        if is_stopped and segment_times[-1] - segment_times[0] >= min_duration:
            detected_stops.append(
                TemporalRangeWithTrajId(segment_times[0], segment_times[-1], traj.id)
            )

        return detected_stops
예제 #2
0
    def get_cell_axis(poly):
      #  t=time.time()
        line=LineString()
        pre_cloud=[]

        angle,center=get_max_x_angle(poly)

        poly=affinity.rotate(poly, angle,center )

        ext=poly.exterior.coords

        decs=0

        for i,seed_point in enumerate(ext):
            pre_cloud_i=[]

            seed_point=Point(seed_point)

            i=.5;
            intersection=poly.exterior.intersection(seed_point)
            sphere=seed_point.buffer(i)
            intersection=poly.exterior.intersection(sphere.exterior)
            while(len(list(intersection))>0):
                arc=Point()
                sphere=seed_point.buffer(i)
                intersection=poly.exterior.intersection(sphere.exterior)
                i+=3
                #i+=.5

                if(len(list(intersection))<=1):
                    continue

                arc=poly.intersection(sphere.exterior)

                if(arc.geom_type=="MultiLineString"):

                    for this_geom in arc.geoms:
                        arc_xy=list(this_geom.coords)

                        for pi in arc_xy:
                            pi=Point(pi)
                            pre_cloud_i.append((round(pi.x,decs),round(pi.y,decs)))

                elif(arc.geom_type=="LineString"):
                    for pi in list(arc.coords):
                        pi=Point(pi) 
                        pre_cloud_i.append((round(pi.x,decs),round(pi.y,decs)))


            pre_cloud.append(pre_cloud_i)


        cloud=MultiPoint(pre_cloud[0])

        for line in pre_cloud:

            line=MultiPoint(line)
            cloud=cloud.union(line)

        ###make poly fit to points cloud
        xs=[]
        ys=[]
        for pi in cloud:
            pi=Point(pi)

            xs.append(pi.x)
            ys.append(pi.y)

        pf=np.poly1d(np.polyfit(xs,ys,25))
        xr=np.linspace(np.min(xs),np.max(xs),100)

        line=[]
        for x in xr:
            pi=Point(x,pf(x))
            line.append(pi)

        line=LineString(line)
        linexy=np.array(line)
        linex=linexy[:,0]
        liney=linexy[:,1]

        line=poly.intersection(line)
        liner=affinity.rotate(line,-angle,center)
        if(liner.geom_type=="MultiLineString"):
            line=LineString()
            for this_geom in liner.geoms:
                if(this_geom.length>line.length):
                    line=this_geom
            liner=line



        linexy=np.array(liner)
        linex=linexy[:,0]
        liney=linexy[:,1]

        x=(np.max(xs)-np.min(xs))/2+np.min(xs)
        pi=Point(x,pf(x))


        center=affinity.rotate(pi,-angle,center)
       # t=time.time()-t
       # print(t)

        return liner,center
            next(cfile)  # skip the headers
            
            
            # Create the map with the miller projection
            m = Basemap(projection='mill',lon_0=0)

            #get shape file of France to check if files inside
           
            if exclude_France=="True":
                m.readshapefile(shape_file,'France')

                for shape in m.France:
                    if shape == m.France[0]:
                        poly = MultiPoint(shape).convex_hull
                    else:
                        poly = poly.union(MultiPoint(shape).convex_hull)

                for row in cfile:
                    if isnumber(row[1]) and isnumber(row[2]) and isnumber(row[3]):
                        x=float(row[3])
                        y=float(row[2])
                        po = Point(m(x,y))
                        if not poly.contains(po):
                            for i in range(0,int(row[1])):
                                lat.append(y)
                                lon.append(x)
            else:
                for row in cfile:
                    if isnumber(row[1]) and isnumber(row[2]) and isnumber(row[3]):
                        for i in range(0,int(row[1])):
                            lat.append(float(row[2]))
        plt.scatter(*start, color="black")
        # plt.scatter(*good_points[1], color = "black")
        # plt.scatter(*good_points[2], color = "black")
        plt.show()

    #   break
    # break

# %%

plt.scatter(*np.array(u1.get_soldiers_pos(False)).T, color="green", alpha=0.1)
plt.scatter(*np.array(u2.get_soldiers_pos(False)).T, color="red", alpha=0.1)

u1_pos = MultiPoint(u1.get_soldiers_pos(False)).convex_hull.buffer(-6)
u2_pos = MultiPoint(u2.get_soldiers_pos(False)).convex_hull.buffer(-6)
total = u1_pos.union(u2_pos)

# C = np.zeros((len(u1.soldiers),len(u2.soldiers))) + 1e32

# for i,s1 in enumerate(u1.soldiers):

for j, s2 in enumerate(u2.soldiers):

    conn = LineString((list(s1.body.position), list(s2.body.position)))
    print(conn.intersects(total))

# %%

# %%

# Prova 2