Example #1
0
def find_nearest_point_on_line(line_pts, pts):

    if type(line_pts) is sg.LineString:
        sg_line = line_pts
    else:
        sg_line = sg.asLineString(line_pts)

    if pts.ndim == 1:
        num_pts = 1
    else:
        num_pts = np.shape(pts)[0]
    if num_pts > 1:
        min_dist = 1000
        for i_pt in range(num_pts):
            test_pt = sg.asPoint(pts[i_pt, :])
            test_dist = test_pt.distance(sg_line)
            if test_dist < min_dist:
                min_dist = test_dist
                closest_pt = test_pt

        sg_point = closest_pt
    else:
        sg_point = sg.asPoint(pts)
    try:
        near_pts = so.nearest_points(sg_line, sg_point)
    except:
        pass

    nndist = near_pts[0].distance(near_pts[1])
    nn_pt = near_pts[0]

    return nndist, nn_pt
Example #2
0
    def test_numpy_adapter(self):
        from numpy import array, asarray
        from numpy.testing import assert_array_equal

        # Adapt a Numpy array to a point
        a = array([1.0, 2.0])
        pa = asPoint(a)
        assert_array_equal(pa.context, array([1.0, 2.0]))
        self.assertEqual(pa.coords[:], [(1.0, 2.0)])

        # Now, the inverse
        self.assertEqual(pa.__array_interface__,
                         pa.context.__array_interface__)

        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 2.0]))

        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        coords[0] = 1.0

        # Now, the inverse (again?)
        self.assertIsNotNone(pa.__array_interface__)
        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 4.0]))
Example #3
0
    def test_numpy(self):

        from numpy import array, asarray
        from numpy.testing import assert_array_equal

        # Construct from a numpy array
        p = Point(array([1.0, 2.0]))
        self.assertEqual(p.coords[:], [(1.0, 2.0)])

        # Adapt a Numpy array to a point
        a = array([1.0, 2.0])
        pa = asPoint(a)
        assert_array_equal(pa.context, array([1.0, 2.0]))
        self.assertEqual(pa.coords[:], [(1.0, 2.0)])

        # Now, the inverse
        self.assertEqual(pa.__array_interface__,
                         pa.context.__array_interface__)

        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 2.0]))

        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        coords[0] = 1.0

        # Now, the inverse (again?)
        self.assertIsNotNone(pa.__array_interface__)
        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 4.0]))

        # From Array.txt
        p = Point(0.0, 0.0, 1.0)
        coords = p.coords[0]
        self.assertEqual(coords, (0.0, 0.0, 1.0))
        self.assertIsNotNone(p.ctypes)

        # Convert to Numpy array, passing through Python sequence
        a = asarray(coords)
        self.assertEqual(a.ndim, 1)
        self.assertEqual(a.size, 3)
        self.assertEqual(a.shape, (3, ))

        # Convert to Numpy array, passing through a ctypes array
        b = asarray(p)
        self.assertEqual(b.size, 3)
        self.assertEqual(b.shape, (3, ))
        assert_array_equal(b, array([0.0, 0.0, 1.0]))

        # Make a point from a Numpy array
        a = asarray([1.0, 1.0, 0.0])
        p = Point(*list(a))
        self.assertEqual(p.coords[:], [(1.0, 1.0, 0.0)])

        # Test array interface of empty geometry
        pe = Point()
        a = asarray(pe)
        self.assertEqual(a.shape[0], 0)
Example #4
0
    def test_numpy(self):

        from numpy import array, asarray
        from numpy.testing import assert_array_equal

        # Construct from a numpy array
        p = Point(array([1.0, 2.0]))
        self.assertEqual(p.coords[:], [(1.0, 2.0)])

        # Adapt a Numpy array to a point
        a = array([1.0, 2.0])
        pa = asPoint(a)
        assert_array_equal(pa.context, array([1.0, 2.0]))
        self.assertEqual(pa.coords[:], [(1.0, 2.0)])

        # Now, the inverse
        self.assertEqual(pa.__array_interface__,
                         pa.context.__array_interface__)

        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 2.0]))

        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        coords[0] = 1.0

        # Now, the inverse (again?)
        self.assertIsNotNone(pa.__array_interface__)
        pas = asarray(pa)
        assert_array_equal(pas, array([1.0, 4.0]))

        # From Array.txt
        p = Point(0.0, 0.0, 1.0)
        coords = p.coords[0]
        self.assertEqual(coords, (0.0, 0.0, 1.0))
        self.assertIsNotNone(p.ctypes)

        # Convert to Numpy array, passing through Python sequence
        a = asarray(coords)
        self.assertEqual(a.ndim, 1)
        self.assertEqual(a.size, 3)
        self.assertEqual(a.shape, (3,))

        # Convert to Numpy array, passing through a ctypes array
        b = asarray(p)
        self.assertEqual(b.size, 3)
        self.assertEqual(b.shape, (3,))
        assert_array_equal(b, array([0.0, 0.0, 1.0]))

        # Make a point from a Numpy array
        a = asarray([1.0, 1.0, 0.0])
        p = Point(*list(a))
        self.assertEqual(p.coords[:], [(1.0, 1.0, 0.0)])

        # Test array interface of empty geometry
        pe = Point()
        a = asarray(pe)
        self.assertEqual(a.shape[0], 0)
Example #5
0
File: map.py Project: csdms/pymt
    def __init__(self, *args, **kwargs):
        super(UnstructuredMap, self).__init__(*args, **kwargs)

        self._point = {}
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            for point_id in cell:
                try:
                    self._point[point_id].append(cell_id)
                except KeyError:
                    self._point[point_id] = [cell_id]

        (point_x, point_y) = (self.get_x(), self.get_y())
        self._polys = []
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            (x, y) = (point_x.take(cell), point_y.take(cell))
            if len(x) > 2:
                self._polys.append(asPolygon(zip(x, y)))
            elif len(x) == 2:
                self._polys.append(asLineString(zip(x, y)))
            else:
                self._polys.append(asPoint(zip(x, y)))
Example #6
0
    def __init__(self, *args, **kwargs):
        super(UnstructuredMap, self).__init__(*args, **kwargs)

        self._point = {}
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            for point_id in cell:
                try:
                    self._point[point_id].append(cell_id)
                except KeyError:
                    self._point[point_id] = [cell_id]

        (point_x, point_y) = (self.get_x(), self.get_y())
        self._polys = []
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            (x, y) = (point_x.take(cell), point_y.take(cell))
            if len(x) > 2:
                self._polys.append(asPolygon(zip(x, y)))
            elif len(x) == 2:
                self._polys.append(asLineString(zip(x, y)))
            else:
                self._polys.append(asPoint(zip(x, y)))
Example #7
0
def boundary_of_drivemap(drivemap, footprint, height=1.0, edge_width=0.25):
    """
    Construct an object boundary using the manually recorded corner points.
    Do this by finding all the points in the drivemap along the footprint.
    Use the bottom 'height' meters of the drivemap (not trees).
    Resulting pointcloud has the same SRS and offset as the input.

    Arguments:
        drivemap   : pcl.PointCloud
        footprint  : pcl.PointCloud
        height     : Cut-off height, points more than this value above the
                     lowest point of the drivemap are considered trees,
                     and dropped. default 1 m.
        edge_width : Points belong to the boundary when they are within
                     this distance from the footprint. default 0.25

    Returns:
        boundary   : pcl.PointCloud
    """

    # construct basemap as the bottom 'height' meters of the drivemap

    drivemap_array = np.asarray(drivemap)
    bb = BoundingBox(points=drivemap_array)
    basemap = extract_mask(drivemap, drivemap_array[:, 2] < bb.min[2] + height)

    # Cut band between +- edge_width around the footprint
    edge = LinearRing(np.asarray(footprint)).buffer(edge_width)
    boundary = extract_mask(basemap,
                            [edge.contains(asPoint(pnt)) for pnt in basemap])

    utils.force_srs(boundary, same_as=basemap)

    return boundary
Example #8
0
def perpendicular_at(line, point, length):
    """
    line: a linestring
    point: a point within the line at which to search for a perpendicular line
    length: length of the line
    """
    point = asPoint(point)
    E = 1e-8
    if line.intersects(point):
        refpoint = point
    else:
        r = 16
        while True:
            refpoint = point.buffer(line.distance(point)+E, resolution=r).exterior.intersection(line)
            if not refpoint.is_empty:
                break
            else:
                r = r * 2
        assert not refpoint.is_empty
    a = line_angle_at(line, refpoint)
    a2 = a + pi/2
    p2 = Point(point.x, point.y + length*0.5)
    p3 = rotate(p2, -math.degrees(a2), origin=point)
    p4 = rotate(p2, (180 - math.degrees(a2)), origin=point)
    l = linestr(p3, p4)
    return l
Example #9
0
    def add_xy_feature(self,
                       x,
                       y,
                       field_record,
                       layer_index=0,
                       layer_name=None):
        """Add a feature from x,y numpy arrays (or anything that can be zipped into coordinate pairs).

        layer_index: index of the layer to which the feature will be added.
        layer_name: overrides layer index
        """

        coords = list(zip(x, y))
        if self.geometry in ['Point', 'point']:
            shape = asPoint(coords)
        elif self.geometry in ['line', 'LineString']:
            shape = asLineString(coords)
        elif self.geometry in [
                'polygon', 'Polygon'
        ]:  # Right now, only simple polygons are supported
            shape = asPolygon(coords)

        self.add_wkt_feature(
            shape.wkt,
            field_record,
            layer_index=layer_index,
            layer_name=layer_name)
Example #10
0
def find_nearest_neighbor(x, y, num_neighbors=1):
    '''

    :param x: object borders as an m x 2 numpy array, where m is the number of points
    :param y: object borders as an n x 2 numpy array, where n is the number of points
    :param num_neighbors:
    :return:
    '''
    if isinstance(x, sg.Point) or isinstance(x, sg.MultiPoint):
        # x was input as a shapely point/multipoint object
        point_x = x
    elif x.ndim == 1 or np.shape(x)[0] == 1:
        # x is a vector, either shape (2,) or shape (1,2)
        point_x = sg.asPoint(np.squeeze(x))
    else:
        # x is an m x 2 array of multiple points (m > 1)
        point_x = sg.asMultiPoint(x)

    if isinstance(y, sg.Point) or isinstance(y, sg.MultiPoint):
        # y was input as a shapely point/multipoint object
        point_y = y
    elif y.ndim == 1 or np.shape(y)[0] == 1:
        # y is a vector, either shape (2,) or shape (1,2)
        point_y = sg.asPoint(np.squeeze(y))
    else:
        # y is an m y 2 array of multiple points (m > 1)
        point_y = sg.asMultiPoint(y)

    # if type(y) is sg.Point:
    #     point_y = y
    # else:
    #     point_y = sg.asPoint(y)

    near_x, near_y = so.nearest_points(point_x, point_y)
    nn_dist = near_x.distance(near_y)

    # left over from when I thought it would be useful to find the n nearest points. Right now, just finds the nearest points
    # pts_diff = y - x
    #
    # dist_from_x = np.linalg.norm(pts_diff, axis=1)
    #
    # sorted_dist_idx = np.argsort(dist_from_x)
    # sorted_dist = np.sort(dist_from_x)
    #
    # return sorted_dist[:num_neighbors], sorted_dist_idx[:num_neighbors]

    return nn_dist, near_y
Example #11
0
 def init_mesh_points(self):
     # ищем точку в пределах границ
     for x in np.arange(self.min_x, self.max_x, self.dx):
         for y in np.arange(self.min_y, self.max_y, self.dy):
             # если точка лежит внутри geom
             if self.geom.contains(asPoint([x, y])):
                 # инициализируем точку и остальные от неё
                 self.init_mesh_point(x, y)
                 return
Example #12
0
def flight_entry(trajectory):

    # interpolate trajectory
    trajectory_interp = interpolate_trajectory(trajectory)
    trajectory_interp.reset_index(inplace=True, drop=True)

    # check whether the first point of a trajectory is within AOR
    # if that is the case, return None
    first_point = asPoint(trajectory_interp.loc[0, ['X', 'Y']].values)
    if first_point.within(aor_polygon):
        # print("trajectory started within AOR")
        return pd.Series([None, None, None], index=['ENTRY_X', 'ENTRY_Y', 'ENTRY_T'])

    # convert trajectory to shapely object
    trajectory_geom = asLineString(trajectory_interp.loc[:, ['X', 'Y']].values)

    # intersect trajectory with AOR
    intersection = trajectory_geom.intersection(aor_geom)
    inters_geom = intersection.geom_type

    if not isinstance(intersection, Point) and not isinstance(intersection, MultiPoint) and not isinstance(intersection, GeometryCollection):
        print bcolors.WARNING + "unknown geometry type" + bcolors.ENDC, inters_geom, '\n'

    if isinstance(intersection, Point):
        # trajectory intersects AOR at one point - entry
        entry_x, entry_y = intersection.x, intersection.y

    elif isinstance(intersection, MultiPoint):
        # trajectory intersects AOR at two points - entry and exit
        # since Shapely intersection method does not return
        # intersection points in the right order (i.e. first
        # intersection close to the start of the trajectory),
        # compute the distance from the first observation
        # to each point and take the one with the smallest distance
        distances = []
        for point in intersection.geoms:
            distances.append(np.sqrt((point.x - trajectory_interp.loc[0, 'X'])**2 + (point.y - trajectory_interp.loc[0, 'Y'])**2))

        entry_point = intersection.geoms[np.argmin(distances)]
        entry_x, entry_y = entry_point.x, entry_point.y

    elif isinstance(intersection, GeometryCollection) and intersection.is_empty:
        # if there is no intersection - there is no entry point
        entry_x, entry_y = None, None

    if entry_x and entry_y:
        # estimate the time of intersection as the time at a point on-route
        # closest to the entry point (i.e. smallest distance)
        trajectory_interp['DIST'] = trajectory_interp.apply(lambda row: np.sqrt((row['X'] - entry_x)**2 + (row['Y'] - entry_y)**2), axis=1)
        entry_t = trajectory_interp.sort_values(by='DIST').iloc[0, :]['TIME_']
    else:
        entry_t = None

    return pd.Series([entry_x, entry_y, entry_t], index=['ENTRY_X', 'ENTRY_Y', 'ENTRY_T'])
    def generate_start_point(self):
        intersections = [True]  # We initialize it with something to run the loop
        origin = [0, 0]

        while any(intersections):
            origin = np.multiply(np.random.uniform(size=2), np.array([self.map_size_x, self.map_size_y]))
            point = asPoint(origin)
            intersections = []
            [intersections.append(obstacle.contains(point)) for obstacle in self.obstacles]

        return [origin[0]], [origin[1]]
Example #14
0
    def test_point_adapter(self):
        p = Point(0.0, 0.0)
        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        self.assertEqual(pa.coords[0], (3.0, 4.0))
        self.assertEqual(pa.distance(p), 5.0)

        # Move the coordinates and watch the distance change
        coords[0] = 1.0
        self.assertEqual(pa.coords[0], (1.0, 4.0))
        self.assertAlmostEqual(pa.distance(p), 4.123105625617661)
def np_to_wkb(np_array, in_server):
    from shapely import wkb

    np_array = np.array(np_array)

    if len(np_array.shape) <= 1:
        #print len(np_array.shape)
        from shapely.geometry import asPoint
        np_shapely = asPoint(np_array)
    else:
        from shapely.geometry import asMultiPoint
        np_shapely = asMultiPoint(np_array)
    return wkb.dumps(np_shapely, hex=in_server)
def np_to_wkb(np_array,in_server):
    from shapely import wkb
    
    np_array = np.array(np_array)
    
    if len(np_array.shape)<=1:
        #print len(np_array.shape)
        from shapely.geometry import asPoint
        np_shapely = asPoint(np_array)
    else:
        from shapely.geometry import asMultiPoint
        np_shapely = asMultiPoint(np_array) 
    return wkb.dumps(np_shapely, hex=in_server)
Example #17
0
def main():
    # If a crime data path and zip data path were given as arguments
    if len(sys.argv) == 3:
        crime_data_path = sys.argv[1]
        zip_data_path = sys.argv[2]
    else:
        try:
            crime_data_path = "C:\Users\Graham\Documents\STA 141C\project\police_department_incidents.csv"
            zip_data_path = "C:\Users\Graham\Documents\STA 141C\project\cb_2015_us_zcta510_500k/cb_2015_us_zcta510_500k.shp"
        except:
            print(
                "Usage: python2 data_setup.py Police_Department_Incidents.csv cb_2015_us_zcta510_500k.shp"
            )

    data = pd.read_csv(crime_data_path)

    # LOAD IN ZIP CODES
    zipshapes = gpd.read_file(zip_data_path)

    # NARROW IT DOWN SOME
    zipshapes = zipshapes[zipshapes.ZCTA5CE10.str.startswith("9")]

    #CUT OUT UNNESSESARY STUFF
    zipshapes = zipshapes[['geometry', 'ZCTA5CE10']]

    points = gpd.GeoDataFrame(
        [geom.asPoint(tuple(row)) for row in data[["X", "Y"]].values],
        columns=["geometry"])
    zipcodes = gpd.sjoin(zipshapes, points, how="inner", op="contains")
    data['index_right'] = range(len(data))
    newdata = data.merge(zipcodes, on=['index_right'])
    newdata['zipcode'] = [str(x) for x in newdata['ZCTA5CE10']]

    #DEFINE CODES THAT RESULTED IN SOME KIND OF PUNATIVE ACTION
    action = [
        'ARREST, BOOKED', 'ARREST, CITED', 'JUVENILE BOOKED',
        'PROSECUTED BY OUTSIDE AGENCY', 'JUVENILE CITED', 'JUVENILE DIVERTED',
        'PROSECTUTED FOR LESSER OFFENSE'
    ]

    action_taken = [x in action for x in newdata['Resolution']]
    newdata['action_taken'] = action_taken

    #DROP THE STUFF WE DON'T CARE ABOUT
    newdata = newdata.drop('index_right', axis=1)
    newdata = newdata.drop('ZCTA5CE10', axis=1)
    newdata = newdata.drop('PdId', axis=1)

    # Export the prepared data frame for use in other scripts
    newdata.to_hdf('crime.h5', 'table', append=False)
Example #18
0
def find_nearest_point_to_line(line_pts, pts):

    if type(line_pts) is sg.LineString:
        sg_line = line_pts
    else:
        sg_line = sg.asLineString(line_pts)

    sg_point = sg.asPoint(pts)

    near_pts = so.nearest_points(sg_line, sg_point)
    nndist = near_pts[0].distance(near_pts[1])
    nn_pt = near_pts[1]

    return nndist, nn_pt
    def generate_target_point(self):
        intersections = [True]  # We initialize it with something to run the loop
        target = [0, 0]
        while any(intersections):
            dist = 0
            while dist < 2 * self.threshold_dist:
                target = np.multiply(np.random.uniform(size=2), np.array([self.map_size_x, self.map_size_y]))
                dist = np.linalg.norm([self.s['origin_x'] - target[0], self.s['origin_y'] - target[1]])

            point = asPoint(target)
            intersections = []
            [intersections.append(obstacle.contains(point)) for obstacle in self.obstacles]

        return [target[0]], [target[1]]
Example #20
0
def calc_ocean_mask(dataset, oceans=oceans, pt_return=False,
                    longitude='lon', latitude='lat'):

    lons, lats = dataset[longitude], dataset[latitude]
    if isinstance(dataset, (xray.Dataset, xray.DataArray)):
        lons.values = shift_lons(lons.values)
    else:
        lons = shift_lons(lons)

    points = [asPoint(point) for point in np.column_stack([lons, lats])]
    if pt_return:
        return points

    in_ocean = [_is_in_ocean(p, oceans) for p in points]
    return in_ocean
Example #21
0
def label_continents(in_fn, out_fn):
    # Load in a low-res dataset with continental boundaries
    # and dissolve borders.
    world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    continents = world[['continent', 'geometry']]
    continents = continents.dissolve(by='continent')
    continents = continents.reset_index()

    # Load in a sample dataset with the lat-lon mesh you want
    # to map labels to
    data = xr.open_dataset(in_fn)
    # For CESM data, fix lons to monotonically increase from
    # [-180, 180]
    data = shift_lons(data).roll(lon=len(data.lon) // 2 - 1)

    # Assume grid gives cell centers; compute Point correspond to each
    # cell center
    llon, llat = np.meshgrid(data.lon.values, data.lat.values)
    lon_lin, lat_lin = llon.ravel(), llat.ravel()
    points = [asPoint(p) for p in np.column_stack([lon_lin, lat_lin])]

    # Coerce to a geopandas data structure
    pts = gpd.GeoDataFrame({
        'lon': lon_lin,
        'lat': lat_lin,
        'geometry': gpd.GeoSeries(points)
    })
    pts.head()

    # Spatial join to map cells to continent labels
    joined = gpd.sjoin(pts, continents, how='inner', op='intersects')
    joined.head()

    # Re-structure by merging back into original dataset and save to disk
    labels = (joined[[
        'lat', 'lon', 'continent'
    ]].assign(continent=lambda x: x.continent.astype('category')).set_index(
        ['lat', 'lon']).sortlevel().assign(
            continent_id=lambda x: x.continent.cat.codes).to_xarray())

    x = (data.copy().merge(labels))

    to_save = x[['continent', 'continent_id']]
    to_save = append_history(to_save)

    to_save.to_netcdf(out_fn, mode='w')
Example #22
0
def mask_ocean_points(dataset, oceans=None, pt_return=False,
                      longitude='lon', latitude='lat'):

    if oceans is None:
        oceans = _get_ocean_shapefile()
    if oceans is None:  # still?!? Must be broken
        raise RuntimeError("Couldn't load default ocean shapefile")

    lons, lats = dataset[longitude], dataset[latitude]
    if isinstance(dataset, (xarray.Dataset, xarray.DataArray)):
        lons.values = shift_lons(lons.values)
    else:
        lons = shift_lons(lons)

    points = [asPoint(point) for point in np.column_stack([lons, lats])]
    if pt_return:
        return points

    in_ocean = [_is_in_ocean(p, oceans) for p in points]
    return in_ocean
Example #23
0
	def submit(self, data):
		logging.debug("submitting %d tweets" % len(data))
		conn = psycopg2.connect("dbname="+PG_DBNAME+" user="******" password="******"parsing tweet...")
			offset=0
			lonlat = []
			for m in llregex.finditer(tweet["location"]):
				lonlat.insert(0,float(m.group()))
			if len(lonlat)<2:
				logging.info("got inexact location: %s - geocoding..." , tweet["location"])
				try:
					g = geocoders.Google()
					place, (lat, lng) = g.geocode(tweet["location"])
					logging.info("geocoded %s as %s, coords %f,%f" % (tweet["location"],place,lng,lat))
					lonlat = [lng,lat]
				except:
					logging.warn("geocoding failed, skipping this tweet...")
					continue
			p = Point(lonlat)
			timestring = str(tweet["created_at"])
			try:
				offset = int(timestring[-5:])
			except:
				logging.debug("Could not parse timezone from %s" % timestring)
			delta = timedelta(hours = offset / 100)
			dt = datetime.strptime(tweet["created_at"][:-6], "%a, %d %b %Y %H:%M:%S")
			dt -= delta
			logging.debug("time from string %s is %s" % (timestring,dt))
			logging.debug(asPoint(lonlat).wkt)
			logging.debug(tweet["text"])
			logging.debug(cur.mogrify("INSERT INTO tweets (id,loc,userid,text,datetime) VALUES (%s, %s, %s, %s, %s)", (int(tweet["id_str"]),p,int(tweet["from_user_id"]),tweet["text"], dt)))
			try:
				cur.execute("INSERT INTO tweets (id,loc,userid,text,datetime) VALUES (%s, %s, %s, %s, %s)", (int(tweet["id_str"]),p,int(tweet["from_user_id"]),tweet["text"], dt))
			except:
				logging.error("insertion failed")
		conn.commit()
		cur.close()
		conn.close()
Example #24
0
    def classify_point(self, x, y):
        #print 'call to classify point'

        reading = asPoint(np.array([x, y]))
        #print reading.wkt

        classification = None

        for poly in self.arc:
            if reading.within(poly):
                classification = 1
                print 'arc'
                break

        for poly in self.bac:
            if reading.within(poly):
                classification = 0
                print 'bac'
                break

        #print classification
        return classification
Example #25
0
def mask_ocean_points(dataset,
                      oceans=None,
                      pt_return=False,
                      longitude='lon',
                      latitude='lat'):

    if oceans is None:
        oceans = _get_ocean_shapefile()
    if oceans is None:  # still?!? Must be broken
        raise RuntimeError("Couldn't load default ocean shapefile")

    lons, lats = dataset[longitude], dataset[latitude]
    if isinstance(dataset, (xarray.Dataset, xarray.DataArray)):
        lons.values = shift_lons(lons.values)
    else:
        lons = shift_lons(lons)

    points = [asPoint(point) for point in np.column_stack([lons, lats])]
    if pt_return:
        return points

    in_ocean = [_is_in_ocean(p, oceans) for p in points]
    return in_ocean
def create_place(item):
    position = item['position']
    lat = position[0]
    lng = position[1]
    # Geography data
    loc_el = WKTElement(asPoint(position).wkt)
    poi = TagPlaces(here_id=item['id'], name=item['title'], category=item['category']['title'], lat=lat, lng=lng,
                    location=loc_el)

    try:
        q = _session.query(TagPlaces).filter(TagPlaces.here_id == item['id'])
        one = q.one()

        if one == None:
            _session.add(poi)
        else:
            poi.id = one.id
            _session.merge(poi)
    except:
        _session.add(poi)
        pass

    _session.commit()
def create_pt_stop(item):
    position = item['position']
    lat = position[0]
    lng = position[1]
    # Geography data
    loc_el = WKTElement(asPoint(position).wkt)

    stop = TagPtStops(here_id=item['id'], name=item['title'], vicinity=item.get('vicinity'), location=loc_el, lat=lat,
                      lng=lng)

    try:
        q = _session.query(TagPtStops).filter(TagPtStops.here_id == item['id'])
        one = q.one()

        if one == None:
            _session.add(stop)
            _session.commit()
        else:
            stop.id = one.id
            # _session.merge(stop)
    except:
        _session.add(stop)
        _session.commit()
        pass
Example #28
0
import geopandas as gpd
from __future__ import division

data = pd.read_csv("police_department_incidents.csv")

# LOAD IN ZIP CODES - from 141B HW 6
zipshapes = gpd.read_file("cb_2015_us_zcta510_500k.shp")

# NARROW IT DOWN SOME
zipshapes = zipshapes[zipshapes.ZCTA5CE10.str.startswith("9")]

# CUT OUT UNNESSESARY STUFF
zipshapes = zipshapes[['geometry', 'ZCTA5CE10']]

points = gpd.GeoDataFrame(
    [geom.asPoint(tuple(row)) for row in data[["X", "Y"]].values],
    columns=["geometry"])
zipcodes = gpd.sjoin(zipshapes, points, how="inner", op="contains")
data['index_right'] = range(len(data))
newdata = data.merge(zipcodes, on=['index_right'])
newdata['zipcode'] = [str(x) for x in newdata['ZCTA5CE10']]

# DEFINE CODES THAT RESULTED IN SOME KIND OF PUNATIVE ACTION
action = [
    'ARREST, BOOKED', 'ARREST, CITED', 'JUVENILE BOOKED',
    'PROSECUTED BY OUTSIDE AGENCY', 'JUVENILE CITED', 'JUVENILE DIVERTED',
    'PROSECTUTED FOR LESSER OFFENSE'
]

action_taken = [x in action for x in newdata['Resolution']]
newdata['action_taken'] = action_taken
    def calculate(self, k=3):
        """
        Calculates the convex hull of the data set as an array of points
        :param k: Number of nearest neighbors
        :return: Array of points (N, 2) with the concave hull of the data set
        """
        if self.data_set.shape[0] < 3:
            return None

        if self.data_set.shape[0] == 3:
            return self.data_set

        # Make sure that k neighbors can be found
        kk = min(k, self.data_set.shape[0])

        first_point = self.get_lowest_latitude_index(self.data_set)
        current_point = first_point

        # Note that hull and test_hull are matrices (N, 2)
        hull = np.reshape(np.array(self.data_set[first_point, :]), (1, 2))
        test_hull = hull

        # Remove the first point
        self.indices[first_point] = False

        prev_angle = 270    # Initial reference id due west. North is zero, measured clockwise.
        step = 2
        stop = 2 + kk

        while ((current_point != first_point) or (step == 2)) and len(self.indices[self.indices]) > 0:
            if step == stop:
                self.indices[first_point] = True

            knn = self.get_k_nearest(current_point, kk)

            # Calculates the headings between first_point and the knn points
            # Returns angles in the same indexing sequence as in knn
            angles = self.calculate_headings(current_point, knn, prev_angle)

            # Calculate the candidate indexes (largest angles first)
            candidates = np.argsort(-angles)

            i = 0
            invalid_hull = True

            while invalid_hull and i < len(candidates):
                candidate = candidates[i]

                # Create a test hull to check if there are any self-intersections
                next_point = np.reshape(self.data_set[knn[candidate]], (1,2))
                test_hull = np.append(hull, next_point, axis=0)

                line = asLineString(test_hull)
                invalid_hull = not line.is_simple
                i += 1

            if invalid_hull:
                return self.recurse_calculate()

            # prev_angle = self.calculate_headings(current_point, np.array([knn[candidate]]))
            prev_angle = self.calculate_headings(knn[candidate], np.array([current_point]))
            current_point = knn[candidate]
            hull = test_hull

            # write_line_string(hull)

            self.indices[current_point] = False
            step += 1

        poly = asPolygon(hull)

        count = 0
        total = self.data_set.shape[0]
        for ix in range(total):
            pt = asPoint(self.data_set[ix, :])
            if poly.intersects(pt) or pt.within(poly):
                count += 1
            else:
                d = poly.distance(pt)
                if d < 1e-5:
                    count += 1

        if count == total:
            return hull
        else:
            return self.recurse_calculate()
Example #30
0
def _add_geo_location(doc):
    doc["geo_location"] = asPoint(array([doc["longitude"], doc["latitude"]])).wkt
    return doc
Example #31
0
def operation_example():
	patch = Point(0.0, 0.0).buffer(10.0)
	print('patch = {}.'.format(patch))
	print('patch.area = {}.'.format(patch.area))

	line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
	dilated = line.buffer(0.5)
	eroded = dilated.buffer(-0.3)

	ring = LinearRing([(0, 0), (1, 1), (1, 0)])

	point = Point(0.5, 0.5)
	polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
	print('polygon.contains(point) = {}.'.format(polygon.contains(point)))

	#box = shapely.geometry.box(minx, miny, maxx, maxy, ccw=True)

	pa = asPoint(np.array([0.0, 0.0]))
	la = asLineString(np.array([[1.0, 2.0], [3.0, 4.0]]))
	ma = asMultiPoint(np.array([[1.1, 2.2], [3.3, 4.4], [5.5, 6.6]]))

	data = {'type': 'Point', 'coordinates': (0.0, 0.0)}
	geom = shapely.geometry.shape(data)
	#geom = shapely.geometry.asShape(data)

	#--------------------
	#geom = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
	geom = LineString([(0, 0), (10, 0), (10, 5), (20, 5)])

	print('polygon.has_z = {}.'.format(geom.has_z))
	if isinstance(geom, LinearRing):
		print('polygon.is_ccw = {}.'.format(geom.is_ccw))  # LinearRing only.
	print('polygon.is_empty = {}.'.format(geom.is_empty))
	print('polygon.is_ring = {}.'.format(geom.is_ring))
	print('polygon.is_simple = {}.'.format(geom.is_simple))
	print('polygon.is_valid = {}.'.format(geom.is_valid))

	print('polygon.coords = {}.'.format(geom.coords))

	print('polygon.area = {}.'.format(geom.area))
	print('polygon.bounds = {}.'.format(geom.bounds))
	print('polygon.length = {}.'.format(geom.length))
	print('polygon.minimum_clearance = {}.'.format(geom.minimum_clearance))
	print('polygon.geom_type = {}.'.format(geom.geom_type))

	print('polygon.boundary = {}.'.format(geom.boundary))
	print('polygon.centroid = {}.'.format(geom.centroid))

	print('polygon.convex_hull.exterior.coords.xy = {}.'.format(list((x, y) for x, y in zip(*geom.convex_hull.exterior.coords.xy))))
	print('polygon.envelope = {}.'.format(geom.envelope))
	print('polygon.minimum_rotated_rectangle = {}.'.format(geom.minimum_rotated_rectangle))

	#polygon.parallel_offset(distance, side, resolution=16, join_style=1, mitre_limit=5.0)
	#polygon.simplify(tolerance, preserve_topology=True)

	#--------------------
	poly1 = Polygon([(3, 3), (5, 3), (5, 5), (3, 5)])
	poly2 = Polygon([(1, 1), (4, 1), (4, 3.5), (1, 3.5)])

	print('poly1.intersection(poly2) = {}.'.format(poly1.intersection(poly2)))
	print('poly1.difference(poly2) = {}.'.format(poly1.difference(poly2)))
	print('poly1.symmetric_difference(poly2) = {}.'.format(poly1.symmetric_difference(poly2)))
	print('poly1.union(poly2) = {}.'.format(poly1.union(poly2)))

	print('poly1.equals(poly2) = {}.'.format(poly1.equals(poly2)))
	print('poly1.almost_equals(poly2, decimal=6) = {}.'.format(poly1.almost_equals(poly2, decimal=6)))
	print('poly1.contains(poly2) = {}.'.format(poly1.contains(poly2)))
	print('poly1.covers(poly2) = {}.'.format(poly1.covers(poly2)))
	print('poly1.crosses(poly2) = {}.'.format(poly1.crosses(poly2)))
	print('poly1.disjoint(poly2) = {}.'.format(poly1.disjoint(poly2)))
	print('poly1.intersects(poly2) = {}.'.format(poly1.intersects(poly2)))
	print('poly1.overlaps(poly2) = {}.'.format(poly1.overlaps(poly2)))
	print('poly1.touches(poly2) = {}.'.format(poly1.touches(poly2)))
	print('poly1.within(poly2) = {}.'.format(poly1.within(poly2)))

	#--------------------
	poly1 = Polygon([(40, 50), (60, 50), (60, 90), (40, 90)])
	poly2 = Polygon([(10, 100), (100, 100), (100, 150), (10, 150)])

	print('The distance between two convex polygons = {}.'.format(poly1.distance(poly2)))
	print('The Hausdorff distance between two convex polygons = {}.'.format(poly1.hausdorff_distance(poly2)))
	print('The representative point of a polygon = {}.'.format(poly1.representative_point()))

	#--------------------
	lines = [
		((0, 0), (1, 1)),
		((0, 0), (0, 1)),
		((0, 1), (1, 1)),
		((1, 1), (1, 0)),
		((1, 0), (0, 0)),
		((1, 1), (2, 0)),
		((2, 0), (1, 0)),
		((5, 5), (6, 6)),
		((1, 1), (100, 100)),
	]

	polys = shapely.ops.polygonize(lines)
	result, dangles, cuts, invalids = shapely.ops.polygonize_full(lines)
	merged_line = shapely.ops.linemerge(lines)

	# Efficient unions.
	polygons = [Point(i, 0).buffer(0.7) for i in range(5)]
	shapely.ops.unary_union(polygons)
	shapely.ops.cascaded_union(polygons)

	# Delaunay triangulation.
	points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
	triangles = shapely.ops.triangulate(points)

	# Voronoi diagram.
	#points = MultiPoint([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
	#regions = shapely.ops.voronoi_diagram(points)

	# Nearest points.
	triangle = Polygon([(0, 0), (1, 0), (0.5, 1), (0, 0)])
	square = Polygon([(0, 2), (1, 2), (1, 3), (0, 3), (0, 2)])
	nearest_points = shapely.ops.nearest_points(triangle, square)

	square = Polygon([(1,1), (2, 1), (2, 2), (1, 2), (1, 1)])
	line = LineString([(0,0), (0.8, 0.8), (1.8, 0.95), (2.6, 0.5)])
	result = shapely.ops.snap(line, square, 0.5)

	g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)])
	g2 = LineString([(5, 0), (30, 0), (30, 5), (0, 5)])
	forward, backward = shapely.ops.shared_paths(g1, g2)

	pt = Point((1, 1))
	line = LineString([(0, 0), (2, 2)])
	result = shapely.ops.split(line, pt)

	ls = LineString((i, 0) for i in range(6))
	shapely.ops.substring(ls, start_dist=1, end_dist=3)
	shapely.ops.substring(ls, start_dist=3, end_dist=1)
	shapely.ops.substring(ls, start_dist=1, end_dist=-3)
	shapely.ops.substring(ls, start_dist=0.2, end_dist=-0.6, normalized=True)
Example #32
0
def adapt_point_wkt(point):
	return AsIs("ST_GeomFromText(%s,4326)" % (adapt(asPoint([point.x,point.y]).wkt)))	
#computing the position of the _center
# E + ( EF + EG )/(norm(EF+EG)) * norm(ET1)/cos(theta)
_center = _e + (_ef + _eg) / (np.linalg.norm(_ef + _eg)) * np.linalg.norm(
    _t1 - _e) / (np.dot(_ef, _eg) /
                 (np.linalg.norm(_ef) * np.linalg.norm(_eg)))
#plpy.notice(_center) ;

t2_g = _e + ((_eg) / np.linalg.norm(_eg)) * np.linalg.norm(_t1 - _e)
t2_f = _e + ((_ef) / np.linalg.norm(_ef)) * np.linalg.norm(_t1 - _e)

if np.linalg.norm(t2_g - _t1) < np.linalg.norm(t2_f - _t1):
    t2__ = t2_f
else:
    t2__ = t2_g

center = wkb.dumps(asPoint(_center), hex=in_server)
radius = _radius
t1 = _t1
t2 = wkb.dumps(asPoint(t2__), hex=in_server)

#plpy.notice(t2) ;
if in_server != True:
    print(center, radius, t1, t2)
else:
    return [center, radius, t1, t2]
    #return { "_center": _center, "_radius": _radius ,  "t1": t1, "t2":t2}
    #return { "_center": _center, "_radius": _radius ,  "t1": t1, "t2":t2}

    #$$ LANGUAGE plpythonu;
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###############################################################################
from shapely.geometry import Point
from numpy import array
array(Point(0, 0))
from shapely.geometry import LineString
from numpy import array
array(LineString([(0, 0), (1, 1)]))
###############################################################################
Point(0, 0).xy
LineString([(0, 0), (1, 1)]).xy
###############################################################################
from shapely.geometry import asPoint
pa = asPoint(array([0.0, 0.0]))
pa.wkt
###############################################################################
from shapely.geometry import asLineString
la = asLineString(array([[1.0, 2.0], [3.0, 4.0]]))
la.wkt
Example #35
0
# -*- coding: utf-8 -*-
import os

from shapely.geometry import Point, LineString
from numpy import array
array(Point(0,0))
array(LineString([(0,0),(1,1)]))

Point(0,0).xy
LineString([(0,0),(1,1)]).xy

from shapely.geometry import asPoint
pa = asPoint(array([(0.0,0.0)]))
# pa.wkt


def Test():
    assert True
Example #36
0
    def calculate(self, k=5):

        #check validity of input
        if self.data_set.shape[0] < 3:
            return None

        if self.data_set.shape[0] == 3:
            return self.data_set

        #starting point
        kk = min(k, self.data_set.shape[0])
        first_point = self.get_lowest_y_index(self.data_set)
        current_point = first_point

        #hull
        hull = np.reshape(np.array(self.data_set[first_point, :]), (1, 2))
        test_hull = hull

        #mask the first point
        self.indices[first_point] = False

        #loop
        prev_angle = 270
        step = 2
        stop = 2 + kk

        while ((current_point != first_point) or
               (step == 2)) and len(self.indices[self.indices]) > 0:
            if step == stop:
                self.indices[first_point] = True

            #indices nearest neighbours
            knn = self.get_k_nearest(current_point, kk)

            #headings between first_point and the knn points, return angles
            angles = self.calculate_headings(current_point, knn, prev_angle)

            #calculate the candidate indexes (largest angles first)
            candidates = np.argsort(-angles)

            i = 0
            invalid_hull = True

            while invalid_hull and i < len(candidates):
                candidate = candidates[i]

                #check for self-intersections
                next_point = np.reshape(self.data_set[knn[candidate]], (1, 2))
                test_hull = np.append(hull, next_point, axis=0)
                line = asLineString(test_hull)
                invalid_hull = not line.is_simple
                i += 1

            if invalid_hull:
                return self.recurse_calculate()

            prev_angle = self.calculate_headings(knn[candidate],
                                                 np.array([current_point]))
            current_point = knn[candidate]
            hull = test_hull

            self.indices[current_point] = False
            step += 1

        poly = asPolygon(hull)

        #check if all points within shape
        count = 0
        total = self.data_set.shape[0]
        for ix in range(total):
            pt = asPoint(self.data_set[ix, :])
            if poly.intersects(pt) or pt.within(poly):
                count += 1
            else:
                d = poly.distance(pt)
                if d < 100:
                    count += 1

        #if all points within
        if count == total:
            return hull
        #if not next iteration
        else:
            return self.recurse_calculate()
Example #37
0
def main():
    for f in FRAME_SOURCE:
        # Load the source we want to run detection on
        video_capture = cv2.VideoCapture(f)

        # Attempt to capture a frame
        success, frame = video_capture.read()
        if success:
            # Convert the image from BGR color (which OpenCV uses) to RGB color
            rgb_image = frame[:, :, ::-1]

            # Run the image through the Mask R-CNN model to get results.
            results = model.detect([rgb_image], verbose=0)

            # Mask R-CNN assumes we are running detection on multiple images.
            # We only passed in one image to detect, so only grab the first result.
            r = results[0]

            # The r variable will now have the results of detection:
            # - r['rois'] are the bounding box of each detected object
            # - r['class_ids'] are the class id (type) of each detected object
            # - r['scores'] are the confidence scores for each detection
            # - r['masks'] are the object masks for each detected object (which gives you the object outline)

            # Filter the results to only grab the car / truck bounding boxes
            car_boxes = get_car_boxes(r['rois'], r['class_ids'])

            print("Cars found in frame of video: ", len(car_boxes))

            # Read clockwise from top-left corner
            poly_coords = ([[751, 1150], [3200, 1140],
                            [3200, 1350], [851, 1400]], [[240, 1140],
                                                         [750, 1150],
                                                         [850, 1400],
                                                         [150, 1400]])

            # BGR colors: Orange, Blue, Red, Gray, Yellow, Cyan, Pink, White
            colors = [[0, 127, 255], [255, 0, 0], [0, 0, 255], [127, 127, 127],
                      [0, 255, 255], [255, 255, 0], [127, 0, 255],
                      [255, 255, 255]]

            # Make an overlay for transparent boxes
            overlay = frame.copy()

            for index, p in enumerate(poly_coords, start=0):
                # Hold count of cars in zone
                count = 0
                # Draw the filled zones
                cv2.fillPoly(overlay, np.int32([np.array(p)]),
                             colors[index + 4])
                # Draw each box on the frame. Do not use rgb_image with cv2!
                for box in car_boxes:
                    # Get the box coordinates
                    y1, x1, y2, x2 = box

                    # Only show cars in the zones!
                    if (((Polygon([
                        (x1, y1), (x2, y1), (x1, y2), (x2, y2)
                    ])).centroid).intersects(Polygon(asPoint(array(p))))):
                        # Draw the box and add to overlay
                        cv2.rectangle(frame, (x1, y1), (x2, y2), colors[index],
                                      5)
                        # Count car in zone
                        count += 1
                        # Delete the car to avoid double counting
                        np.delete(car_boxes, box)

                print("Total cars in zone {}: {}".format(
                    poly_coords.index(p), count))

            # Set transparency for boxes
            alpha = 0.4
            # Add overlay to frame
            frame = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)

            # Resize image if necessary
            scaling = int(
                (768 * 100) / frame.shape[0]) if frame.shape[0] > 768 else 100
            width = int(frame.shape[1] * scaling / 100)
            height = int(frame.shape[0] * scaling / 100)
            dim = (width, height)
            frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)

            # Draw center crosshair
            height, width, channels = frame.shape
            cv2.drawMarker(frame, (int(width / 2), int(height / 2)),
                           [255, 255, 0], cv2.MARKER_TRIANGLE_UP, 16, 2,
                           cv2.LINE_4)
            # Add timestamp
            cv2.putText(frame, timestamp, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                        1, [0, 0, 255], 1)

            # Show the frame of video on the screen
            print("Click on the image window and press enter to continue...")
            cv2.imshow('Video', frame)
            # Hit any key to quit
            cv2.waitKey(0)

        else:
            print("Cannot access image or video!")

    # Clean up everything when finished
    video_capture.release()
    cv2.destroyAllWindows()

    print("Job complete. Have an excellent day.")
Example #38
0
    def test_point(self):

        # Test 2D points
        p = Point(1.0, 2.0)
        self.assertEqual(p.x, 1.0)
        self.assertEqual(p.y, 2.0)
        self.assertEqual(p.coords[:], [(1.0, 2.0)])
        self.assertEqual(str(p), p.wkt)
        self.assertFalse(p.has_z)
        with self.assertRaises(DimensionError):
            p.z

        # Check 3D
        p = Point(1.0, 2.0, 3.0)
        self.assertEqual(p.coords[:], [(1.0, 2.0, 3.0)])
        self.assertEqual(str(p), p.wkt)
        self.assertTrue(p.has_z)
        self.assertEqual(p.z, 3.0)

        # From coordinate sequence
        p = Point((3.0, 4.0))
        self.assertEqual(p.coords[:], [(3.0, 4.0)])

        # From another point
        q = Point(p)
        self.assertEqual(q.coords[:], [(3.0, 4.0)])

        # Coordinate access
        self.assertEqual(p.x, 3.0)
        self.assertEqual(p.y, 4.0)
        self.assertEqual(tuple(p.coords), ((3.0, 4.0), ))
        self.assertEqual(p.coords[0], (3.0, 4.0))
        with self.assertRaises(IndexError):  # index out of range
            p.coords[1]

        # Bounds
        self.assertEqual(p.bounds, (3.0, 4.0, 3.0, 4.0))

        # Geo interface
        self.assertEqual(p.__geo_interface__, {
            'type': 'Point',
            'coordinates': (3.0, 4.0)
        })

        # Modify coordinates
        p.coords = (2.0, 1.0)
        self.assertEqual(p.__geo_interface__, {
            'type': 'Point',
            'coordinates': (2.0, 1.0)
        })

        # Alternate method
        p.coords = ((0.0, 0.0), )
        self.assertEqual(p.__geo_interface__, {
            'type': 'Point',
            'coordinates': (0.0, 0.0)
        })

        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        self.assertEqual(pa.coords[0], (3.0, 4.0))
        self.assertEqual(pa.distance(p), 5.0)

        # Move the coordinates and watch the distance change
        coords[0] = 1.0
        self.assertEqual(pa.coords[0], (1.0, 4.0))
        self.assertAlmostEqual(pa.distance(p), 4.123105625617661)

        # Test Non-operability of Null geometry
        p_null = Point()
        self.assertEqual(p_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
        self.assertEqual(p_null.coords[:], [])
        self.assertEqual(p_null.area, 0.0)

        # Check that we can set coordinates of a null geometry
        p_null.coords = (1, 2)
        self.assertEqual(p_null.coords[:], [(1.0, 2.0)])

        # Passing > 3 arguments to Point is erroneous
        with self.assertRaises(TypeError):
            Point(1.0, 2.0, 3.0, 4.0)
Example #39
0
def regional_intersect_map_geo(parser, griddef, verbose=True):
    '''
    For each pixel, find all gridcells that it intersects

    This function does not compute or save the fractional
    overlap of individual pixels.  It simply stores the
    pixel indices themselves.
    
    This function is currently not configured to operate 
    on a global scale, or near discontinuities. 
    The current kludge to handle discontinuities is 
    relies on the assumption that any pixel of interest 
    will have at least one corner within the bounds of 
    the grid

    Pixels with NaN for any vertex are rejected
    
    Several assumptions are made:
        - Straight lines in projected space adequately 
        approximate the edges of pixels/gridcells.
        - polar discontinuities aren't of concern
        - we AREN'T dealing with a global projection
        - grid is rectilinear
        - pixels are convex polygons
    '''
    
    if verbose:
        print('Mapping '+parser.name+'\nat '+str(datetime.datetime.now()))
    outer_indices = griddef.indLims()
    map = map_helpers.init_output_map(outer_indices)
    map['parser'] = parser
    bounds = prep(map_helpers.rect_bound_poly(outer_indices))
    # we're going to hold onto both the prepared and unprepared versions
    # of the polys, so we can access the fully method set in the unprep
    # polys, but still do fast comparisons
    gridPolys = map_helpers.rect_grid_polys(outer_indices)
    prepPolys = map_helpers.rect_grid_polys(outer_indices)
    if verbose: print('prepping polys in grid')
    for poly in prepPolys.itervalues():
        poly = prep(poly)  # prepare these, they're going to get compared a lot
    if verbose: print('done prepping polys in grid')
    cornersStruct = parser.get_geo_corners()
    (row, col) = griddef.geoToGridded(cornersStruct['lat'], \
                                      cornersStruct['lon']) 
    ind = cornersStruct['ind']
    # reshape the matrixes to make looping workable
    row = row.reshape(-1,4)
    col = col.reshape(-1,4)
    ind = ind.reshape(row.shape[0],-1)
    if verbose:
        griddedPix = 0 
        print('Intersecting pixels')
        sys.stdout.write("Approximately 0 pixels gridded. ")
        sys.stdout.flush()
        for (pxrow, pxcol, pxind) in izip(row, col, ind):
            if numpy.any(numpy.isnan(pxrow)) or numpy.any(numpy.isnan(pxcol)):
                continue  # if we have only a partial pixel, skip
            elif not any([bounds.contains(geom.asPoint((r,c))) \
                       for (r,c) in izip(pxrow, pxcol)]):
                continue  # if none of the corners are in bounds, skip
            griddedPix += 1
            sys.stdout.write("\rApproximately {0} pixels gridded. ".\
                             format(griddedPix))
            sys.stdout.flush()
            pixPoly = geom.MultiPoint(zip(pxrow, pxcol)).convex_hull
            
            for key in map_helpers.get_possible_cells(outer_indices, pixPoly):
                if prepPolys[key].intersects(pixPoly) and not \
                                  gridPolys[key].touches(pixPoly) :
                    map[key].append((tuple(pxind), None))
        print('Done intersecting.')
    else:
        for (pxrow, pxcol, pxind) in izip(row, col, ind):
            if numpy.any(numpy.isnan(pxrow)) or numpy.any(numpy.isnan(pxcol)):
                continue  # if we have only a partial pixel, skip
            elif not any([bounds.contains(geom.asPoint((r,c))) for (r,c) \
                                        in izip(pxrow, pxcol)]):
                continue  # if none of the corners are in bounds, skip
            pixPoly = geom.MultiPoint(zip(pxrow, pxcol)).convex_hull
            for key in map_helpers.get_possible_cells(outer_indices, pixPoly):
                if prepPolys[key].intersects(pixPoly) and not \
                                  gridPolys[key].touches(pixPoly):
                    map[key].append((tuple(pxind), None))
    return map
Example #40
0
    def test_point(self):

        # Test 2D points
        p = Point(1.0, 2.0)
        self.assertEqual(p.x, 1.0)
        self.assertEqual(p.y, 2.0)
        self.assertEqual(p.coords[:], [(1.0, 2.0)])
        self.assertEqual(str(p), p.wkt)
        self.assertFalse(p.has_z)
        with self.assertRaises(DimensionError):
            p.z

        # Check 3D
        p = Point(1.0, 2.0, 3.0)
        self.assertEqual(p.coords[:], [(1.0, 2.0, 3.0)])
        self.assertEqual(str(p), p.wkt)
        self.assertTrue(p.has_z)
        self.assertEqual(p.z, 3.0)

        # From coordinate sequence
        p = Point((3.0, 4.0))
        self.assertEqual(p.coords[:], [(3.0, 4.0)])

        # From another point
        q = Point(p)
        self.assertEqual(q.coords[:], [(3.0, 4.0)])

        # Coordinate access
        self.assertEqual(p.x, 3.0)
        self.assertEqual(p.y, 4.0)
        self.assertEqual(tuple(p.coords), ((3.0, 4.0),))
        self.assertEqual(p.coords[0], (3.0, 4.0))
        with self.assertRaises(IndexError):  # index out of range
            p.coords[1]

        # Bounds
        self.assertEqual(p.bounds, (3.0, 4.0, 3.0, 4.0))

        # Geo interface
        self.assertEqual(p.__geo_interface__,
                         {'type': 'Point', 'coordinates': (3.0, 4.0)})

        # Modify coordinates
        p.coords = (2.0, 1.0)
        self.assertEqual(p.__geo_interface__,
                         {'type': 'Point', 'coordinates': (2.0, 1.0)})

        # Alternate method
        p.coords = ((0.0, 0.0),)
        self.assertEqual(p.__geo_interface__,
                         {'type': 'Point', 'coordinates': (0.0, 0.0)})

        # Adapt a coordinate list to a point
        coords = [3.0, 4.0]
        pa = asPoint(coords)
        self.assertEqual(pa.coords[0], (3.0, 4.0))
        self.assertEqual(pa.distance(p), 5.0)

        # Move the coordinates and watch the distance change
        coords[0] = 1.0
        self.assertEqual(pa.coords[0], (1.0, 4.0))
        self.assertAlmostEqual(pa.distance(p), 4.123105625617661)

        # Test Non-operability of Null geometry
        p_null = Point()
        self.assertEqual(p_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
        self.assertEqual(p_null.coords[:], [])
        self.assertEqual(p_null.area, 0.0)

        # Check that we can set coordinates of a null geometry
        p_null.coords = (1, 2)
        self.assertEqual(p_null.coords[:], [(1.0, 2.0)])

        # Passing > 3 arguments to Point is erroneous
        with self.assertRaises(TypeError):
            Point(1.0, 2.0, 3.0, 4.0)
Example #41
0
import shapely.geometry as geom
import geopandas as gpd
from __future__ import division

data = pd.read_csv("C:\Users\Graham\Documents\STA 141C\project\police_department_incidents.csv")

# LOAD IN ZIP CODES
zipshapes = gpd.read_file("C:\Users\Graham\Documents\STA 141C\project\cb_2015_us_zcta510_500k/cb_2015_us_zcta510_500k.shp")

# NARROW IT DOWN SOME
zipshapes = zipshapes[zipshapes.ZCTA5CE10.str.startswith("9")]

#CUT OUT UNNESSESARY STUFF
zipshapes = zipshapes[['geometry', 'ZCTA5CE10']]

points = gpd.GeoDataFrame([geom.asPoint(tuple(row)) for row in data[["X","Y"]].values], columns=["geometry"])
zipcodes = gpd.sjoin(zipshapes, points, how="inner", op="contains")
data['index_right'] = range(len(data))
newdata = data.merge(zipcodes, on=['index_right'])
newdata['zipcode'] = [str(x) for x in newdata['ZCTA5CE10']]

#DEFINE CODES THAT RESULTED IN SOME KIND OF PUNATIVE ACTION
action = ['ARREST, BOOKED', 'ARREST, CITED', 'JUVENILE BOOKED', 'PROSECUTED BY OUTSIDE AGENCY', 'JUVENILE CITED', 'JUVENILE DIVERTED'
         , 'PROSECTUTED FOR LESSER OFFENSE']

action_taken = [x in action for x in newdata['Resolution']]
newdata['action_taken'] = action_taken

#DROP THE STUFF WE DON'T CARE ABOUT
newdata = newdata.drop('index_right', axis=1)
newdata = newdata.drop('ZCTA5CE10', axis=1)
	#plpy.notice(theta) ; 
	
	#computing the position of the _center
	# E + ( EF + EG )/(norm(EF+EG)) * norm(ET1)/cos(theta)
_center = _e + (_ef+_eg) / (np.linalg.norm(_ef+_eg) ) * np.linalg.norm(_t1-_e) / (np.dot(_ef,_eg)/(np.linalg.norm(_ef)*np.linalg.norm(_eg)) );
	#plpy.notice(_center) ;  
	
t2_g = _e + ((_eg)/np.linalg.norm(_eg)) * np.linalg.norm(_t1-_e) ;
t2_f = _e + ((_ef)/np.linalg.norm(_ef)) * np.linalg.norm(_t1-_e) ;

if np.linalg.norm(t2_g - _t1) < np.linalg.norm(t2_f - _t1) :
	t2__ = t2_f  ;
else :
	t2__ = t2_g  ;

center = wkb.dumps(asPoint(_center), hex=in_server) ;
radius = _radius ;
t1 = _t1 ;
t2 = wkb.dumps(asPoint(t2__), hex=in_server) ;
    
    
#plpy.notice(t2) ; 
if in_server != True :  
	print( center  , radius  , t1  , t2)
else :
	return [center,radius, t1,t2 ];
	#return { "_center": _center, "_radius": _radius ,  "t1": t1, "t2":t2}
	#return { "_center": _center, "_radius": _radius ,  "t1": t1, "t2":t2}
	
     #$$ LANGUAGE plpythonu;