Example #1
0
def plot_xgraph():
    newline = ["\n"]
    p = gcore.Popen(["xgraph"], stdin=gcore.PIPE)
    for point in sine_cosine_replic + newline + outercircle + newline + vector:
        if isinstance(point, tuple):
            p.stdin.write(gcore.encode("%f %f\n" % point))
        else:
            p.stdin.write(gcore.encode(point + "\n"))
    p.stdin.close()
    p.wait()
Example #2
0
    def CalcBoundsFromPoints(self, lats, lngs):
        """Calculates the max/min lat/lng in the lists.

        This method takes in a list of lats and a list of lngs, and
        outputs the southwest and northeast bounds for these points.
        We use this method when we have done a search for points on the
        map, and we get multiple results. In the results we don't get a
        bounding box so this method calculates it for us.

        param: list lats: list of latitudes
        param: lsit lngs: list of longitudes

        returns list: a list of length 2, each holding a list of
        length 2. It holds the southwest and northeast lat/lng
        bounds of a map.  It should look like this:
        [[southwestLat, southwestLng], [northeastLat, northeastLng]]
        """
        lats = [float(x) for x in lats]
        lngs = [float(x) for x in lngs]
        flats = list(map(float, lats))
        flngs = list(map(float, lngs))
        west = min(flngs)
        east = max(flngs)
        north = max(flats)
        south = min(flats)

        coords = "{bottom_left}\n{top_right}".format(
            bottom_left="{} {}".format(west, south),
            top_right="{} {}".format(east, north),
        )

        proc = grass.start_command(
            "m.proj",
            flags="do",
            input="-",
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
        )
        result, error = proc.communicate(input=grass.encode(coords))

        bounds = []
        for row in grass.decode(result).split("\n"):
            if row:
                x, y, z = row.split("|")
                bounds.append([float(x), float(y)])

        return bounds
Example #3
0
def plot_dgraph():
    # use d.info and d.frame to create a square frame in the center of the
    # window.
    s = gcore.read_command("d.info", flags="d")
    f = s.split()
    frame_width = float(f[2])
    frame_height = float(f[3])

    # take shorter side as length of frame side
    min_side = min(frame_width, frame_height)
    dx = (frame_width - min_side) / 2
    dy = (frame_height - min_side) / 2

    fl = dx
    fr = frame_width - dx
    ft = dy
    fb = frame_height - dy

    tenv = os.environ.copy()
    tenv["GRASS_RENDER_FRAME"] = "%f,%f,%f,%f" % (ft, fb, fl, fr)

    # polyline calculations
    ring = 0.95
    scaleval = ring * totalvalidnumber / totalnumber

    sine_cosine_replic_normalized = [((scaleval * p[0] / maxradius + 1) * 50,
                                      (scaleval * p[1] / maxradius + 1) * 50)
                                     for p in sine_cosine_replic
                                     if isinstance(p, tuple)]

    # create circle
    circle = [(
        50 * (1 + ring * math.sin(math.radians(i))),
        50 * (1 + ring * math.cos(math.radians(i))),
    ) for i in range(0, 361)]

    # trend vector
    vect = [((scaleval * p[0] / maxradius + 1) * 50,
             (scaleval * p[1] / maxradius + 1) * 50) for p in vector[1:]]

    # Possible TODOs:
    # To fill data area with color, use BOTH d.graph's polyline and polygon commands.
    # Using polygon alone gives a jagged boundary due to sampling technique
    # (not a bug).

    # plot it!
    lines = ([
        # draw circle
        #   mandatory when drawing proportional to non-square frame
        "color 180:255:180",
        "polyline",
    ] + circle + [
        # draw axes
        "color 180:180:180",
        "width 0",
        "move 0 50",
        "draw 100 50",
        "move 50 0",
        "draw 50 100",
        # draw the goods
        "color red",
        "width 0",
        "polyline",
    ] + sine_cosine_replic_normalized + [
        # draw vector
        "color blue",
        "width 3",
        "polyline",
    ] + vect + [
        # draw compass text
        "color black",
        "width 2",
        "size 10 10",
        "move 51 97",
        "text N",
        "move 51 1",
        "text S",
        "move 1 51",
        "text W",
        "move 97 51",
        "text E",
        # draw legend text
        "width 0",
        "size 10",
        "color 0:180:0",
        "move 0.5 96.5",
        "text All data (incl. NULLs)",
        "color red",
        "move 0.5 93.5",
        "text Real data angles",
        "color blue",
        "move 0.5 90.5",
        "text Avg. direction",
    ])

    p = gcore.feed_command("d.graph", env=tenv)
    for point in lines:
        if isinstance(point, tuple):
            p.stdin.write(gcore.encode("%f %f\n" % point))
        else:
            p.stdin.write(gcore.encode(point + "\n"))
    p.stdin.close()
    p.wait()