Example #1
0
    def update(self, transect):
        """
        Updates the container data to a profile that intersect the
        transect line.

        Returns nothing. Sets attributes as a side effect.

        Args:
            transect (LineString): A transect line.
        """
        Notice.info("Updating " + self.__class__.__name__)

        pad = self.settings['map_padding']
        aspect = 12/3.  # I was expecting it to be 8:3.

        # Calculate the map bounds and centre.
        bounds = transect.bounds
        llx, lly, urx, ury = bounds
        w, h = urx - llx, ury - lly

        x_adj, y_adj = 0, 0
        if h > (w/aspect):
            x_adj = ((aspect*h) - w) / 2.  # Aspect is hard-coded in uberplot
        else:
            y_adj = ((w/aspect) - h) / 2.

        utm_nad83 = pp.Proj("+init=EPSG:26920")
        ll_nad83 = pp.Proj("+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs")
        utm2lola = partial(pp.transform, utm_nad83, ll_nad83)

        ll = transform(utm2lola, Point(llx-pad-x_adj, lly-pad-y_adj))
        ur = transform(utm2lola, Point(urx+pad+x_adj, ury+pad+y_adj))
        self.ll, self.ur = ll, ur
        self.mid = Point(ll.x + 0.5*(ur.x-ll.x), ll.y + 0.5*(ur.y - ll.y))

        # Go over the layers and collect data.
        for layer, details in self.layers.items():
            path = details['file']
            print layer, path

            # Set up convenient params dictionary for plotting function.
            params = {k: v for k, v in details.items() if k != 'file'}
            self.layers[layer]['params'] = params

            # Get a list of shapes from the file.
            shapes = []
            fname, ext = os.path.splitext(os.path.basename(path))
            if ext.strip('.').lower() in self.settings['raster_extensions']:
                # TODO: Deal with rasters.
                pass
            elif ext.strip('.').lower() == 'shp':
                with fiona.open(path) as c:
                    for s in c:
                        shapes.append(shape(s['geometry']))
                        # name = s.get('name') or s.get('id') or None
                        # data = {name: shape(s['geometry'])}
                        # setattr(self, 'data', data)
                setattr(self, layer, shapes)
            else:
                pass
Example #2
0
def getDistance(p1,p2,unit="m",p1_proj="EPSG:4326",p2_proj="EPSG:4326"):
    if p1_proj == 'aea':
        p1_aea = p1
    else:
        p1_aea = ops.transform(
            partial(
                pyproj.transform,
                pyproj.Proj(init=p1_proj),
                #pyproj.Proj(proj="aea",lat1=geometry.bounds[1],lat2=geometry.bounds[3])
                #use projection 'Albers Equal Conic Area for WA' to calcuate the area
                proj_aea(p1)
            ),
            p1
        )

    if p2_proj == 'aea':
        p2_aea = p2
    else:
        p2_aea = ops.transform(
            partial(
                pyproj.transform,
                pyproj.Proj(init=p2_proj),
                #pyproj.Proj(proj="aea",lat1=geometry.bounds[1],lat2=geometry.bounds[3])
                #use projection 'Albers Equal Conic Area for WA' to calcuate the area
                proj_aea(p2)
            ),
            p2
        )

    data = p1_aea.distance(p2_aea)
    if unit == "km" :
        return data / 1000.00 
    else:
        return data
    def test_drop_label(self):
        from shapely.ops import transform
        from tilequeue.tile import reproject_mercator_to_lnglat
        import math
        import dsl

        thresholds = {
            8:    200000000,
            9:    100000000,
            10:    10000000,
            11:     4000000,
            12:      750000,
            13:      100000,
            14:       50000,
            15:       10000,
        }

        for zoom in range(8, 16):
            area = thresholds.get(zoom)
            radius = math.sqrt(area / math.pi)

            coord = 2 ** (zoom - 1)

            # larger feature should retain name
            shape = dsl.tile_centre_shape(
                zoom, coord, coord).buffer(radius * 1.1)
            shape_lnglat = transform(
                    reproject_mercator_to_lnglat, shape)

            self.generate_fixtures(
                dsl.way(1, shape_lnglat, {
                    'natural': 'water',
                    'name': 'Foo',
                }),
            )

            self.assert_has_feature(
                zoom, coord, coord, 'water', {
                    'kind': 'water',
                    'name': 'Foo',
                })

            # smaller shape should drop it
            shape = dsl.tile_centre_shape(
                zoom, coord, coord).buffer(radius / 1.1)
            shape_lnglat = transform(
                    reproject_mercator_to_lnglat, shape)

            self.generate_fixtures(
                dsl.way(1, shape_lnglat, {
                    'natural': 'water',
                    'name': 'Foo',
                }),
            )

            self.assert_has_feature(
                zoom, coord, coord, 'water', {
                    'kind': 'water',
                    'name': type(None),
                })
Example #4
0
def doPolygonize():
  blocks = polygonize(lines)
  writeBlocks(blocks, args[0] + '-blocks.geojson')

  blocks = polygonize(lines)
  bounds = Polygon([
    [minlng, minlat],
    [minlng, maxlat],
    [maxlng, maxlat],
    [maxlng, minlat],
    [minlng, minlat]
  ])
  # Geometry transform function based on pyproj.transform
  project = partial(
    pyproj.transform,
    pyproj.Proj(init='EPSG:3785'),
    pyproj.Proj(init='EPSG:4326'))
  print bounds
  print transform(project, bounds)

  print 'finding holes'
  for index, block in enumerate(blocks):
    if index % 1000 == 0:
      print "diff'd  %s" % (index)
    if not block.is_valid:
      print explain_validity(block)
      print transform(project, block)
    else:
      bounds = bounds.difference(block)
  print bounds
Example #5
0
def fit_in_tile(z, x, y, shape):
    """
    Fit shape into the tile. Shape should be a Shapely geometry or WKT string
    with coordinates between 0 and 1. This unit square is then remapped into
    the tile z/x/y.
    """

    from ModestMaps.Core import Coordinate
    from shapely.ops import transform
    from shapely.wkt import loads as wkt_loads
    from tilequeue.tile import coord_to_mercator_bounds
    from tilequeue.tile import reproject_mercator_to_lnglat

    bounds = coord_to_mercator_bounds(Coordinate(zoom=z, column=x, row=y))

    if isinstance(shape, (str, unicode)):
        shape = wkt_loads(shape)

    # check shape fits within unit square, so we can transform it to fit
    # within the tile.
    assert shape.bounds[0] >= 0
    assert shape.bounds[1] >= 0
    assert shape.bounds[2] <= 1
    assert shape.bounds[3] <= 1

    def _transform(x, y, *unused_coords):
        return (
            x * (bounds[2] - bounds[0]) + bounds[0],
            y * (bounds[3] - bounds[1]) + bounds[1],
        )

    merc_shape = transform(_transform, shape)
    return transform(reproject_mercator_to_lnglat, merc_shape)
Example #6
0
def getDistance(p1,p2,unit="m",p1_proj="EPSG:4326",p2_proj="EPSG:4326"):
    if p1_proj == 'aea':
        p1_aea = p1
    else:
        p1_aea = ops.transform(
            partial(
                pyproj.transform,
                pyproj.Proj(init=p1_proj),
                #pyproj.Proj(proj="aea",lat1=geometry.bounds[1],lat2=geometry.bounds[3])
                #use projection 'Albers Equal Conic Area for WA' to calcuate the area
                pyproj.Proj("+proj=aea +lat_1=-17.5 +lat_2=-31.5 +lat_0=0 +lon_0=121 +x_0=5000000 +y_0=10000000 +ellps=GRS80 +units=m +no_defs ",lat1=p1.bounds[1],lat2=p1.bounds[3])
            ),
            p1
        )

    if p2_proj == 'aea':
        p2_aea = p2
    else:
        p2_aea = ops.transform(
            partial(
                pyproj.transform,
                pyproj.Proj(init=p2_proj),
                #pyproj.Proj(proj="aea",lat1=geometry.bounds[1],lat2=geometry.bounds[3])
                #use projection 'Albers Equal Conic Area for WA' to calcuate the area
                pyproj.Proj("+proj=aea +lat_1=-17.5 +lat_2=-31.5 +lat_0=0 +lon_0=121 +x_0=5000000 +y_0=10000000 +ellps=GRS80 +units=m +no_defs ",lat1=p2.bounds[1],lat2=p2.bounds[3])
            ),
            p2
        )

    data = p1_aea.distance(p2_aea)
    if unit == "km" :
        return data / 1000.00 
    else:
        return data
Example #7
0
def apply_map_projection(polygon, meta_data, projection):
    m = Basemap(llcrnrlon=-50.,llcrnrlat=-50.,urcrnrlon=340.,urcrnrlat=65.,\
                resolution='h',projection=projection,
                lat_0=40.,lon_0=-20.,lat_ts=20.)
    polygon = ops.transform(m, polygon)
    meta_data.POINTS = [ops.transform(m, p) for p in meta_data.POINTS]
    meta_data.LONG = [p.coords[0][0] for p in meta_data.POINTS]
    meta_data.LAT = [p.coords[0][1] for p in meta_data.POINTS]

    return polygon, meta_data
def network_copy_offset(DG,distance=0.0):
    '''copies a network and shifts the subcatchment and stream coordinates by a distance
    
    used for testing conflation simpily using only one network
    Can be used to provide an understanding of the sensitivity of the network to positional error in features'''
    DG2 = DG.copy()
    if distance != 0:
        for f,t,k,data in DG2.edges_iter(data=True,keys=True):
            data['subCatch'] = transform(lambda x, y, z=None: (x+distance, y+distance), data['subCatch'])
            data['stream'] =  transform(lambda x, y, z=None: (x+distance, y+distance), data['stream'])
    return DG2
Example #9
0
    def __getitem__(self, geometry):
        if isinstance(geometry, BaseGeometry) or getattr(geometry, "__geo_interface__", None) is not None:
            g = shape(geometry)
            if g.disjoint(shape(self)):
                raise ValueError("AOI does not intersect image: {} not in {}".format(g.bounds, self.bounds))
            bounds = ops.transform(self.__geo_transform__.rev, g).bounds
            result, xmin, ymin = self._slice_padded(bounds)
        else:
            if len(geometry) == 1:
                assert geometry[0] == Ellipsis
                return self

            elif len(geometry) == 2:
                arg0, arg1 = geometry
                if isinstance(arg1, slice):
                    assert arg0 == Ellipsis
                    return self[:, :, arg1.start:arg1.stop]
                elif arg1 == Ellipsis:
                    return self[arg0, :, :]

            elif len(geometry) == 3:
                try:
                    nbands, ysize, xsize = self.shape
                except:
                    ysize, xsize = self.shape
                band_idx, y_idx, x_idx = geometry
                if y_idx == Ellipsis:
                    y_idx = slice(0, ysize)
                if x_idx == Ellipsis:
                    x_idx = slice(0, xsize)
                if not(isinstance(y_idx, slice) and isinstance(x_idx, slice)):
                    di = DaskImage(self)
                    return di.__getitem__(geometry)
                xmin, ymin, xmax, ymax = x_idx.start, y_idx.start, x_idx.stop, y_idx.stop
                xmin = 0 if xmin is None else xmin
                ymin = 0 if ymin is None else ymin
                xmax = xsize if xmax is None else xmax
                ymax = ysize if ymax is None else ymax
                if ymin > ysize and xmin > xsize:
                    raise IndexError("Index completely out of image bounds")

                g = ops.transform(self.__geo_transform__.fwd, box(xmin, ymin, xmax, ymax))
                result = super(GeoDaskImage, self).__getitem__(geometry)

            else:
                return super(GeoDaskImage, self).__getitem__(geometry)

        gi = mapping(g)
        gt = self.__geo_transform__ + (xmin, ymin)
        image = super(GeoDaskImage, self.__class__).__new__(self.__class__, result, __geo_interface__ = gi, __geo_transform__ = gt)
        return image
Example #10
0
def draw_preview_plot(polygon):
    ax, fig = new_fig()
    m = Basemap(llcrnrlon=-20., llcrnrlat=-50.,
                urcrnrlon=330., urcrnrlat=65.,
                rsphere=(6378137.00, 6356752.3142),
                resolution='h', projection='merc',
                lat_0=40., lon_0=-20., lat_ts=20.)
    m.drawcoastlines()
    m.fillcontinents()

    ops.transform(m, polygon)
    plot_mpp(ax, polygon, fc="red")

    finalize_plot(fig, ax)
Example #11
0
File: map.py Project: aleaf/Figures
    def _load_shapefile(self, shp, index_field, convert_coordinates, remove_offset, simplify):

        df = shp2df(shp)

        if index_field is not None:
            df.index = df[index_field]

        proj4 = get_proj4(shp)

        if proj4 != self.proj4:
            df['geometry'] = projectdf(df, proj4, self.proj4)

        # convert projected coordinate units and/or get rid z values if the shapefile has them
        if convert_coordinates != 1 or df.iloc[0]['geometry'].has_z:
            df['geometry'] = [transform(lambda x, y, z=None: (x * convert_coordinates,
                                                              y * convert_coordinates), g)
                              for g in df.geometry]

        # remove model offset from projected coordinates (llcorner = 0,0)
        if remove_offset:
            df['geometry'] = [translate(g,
                                        -1 * self.extent_proj[0],
                                        -1 * self.extent_proj[1]) for g in df.geometry]

        if simplify > 0:
            df['geometry'] = [g.simplify(simplify) for g in df.geometry]
        return df
Example #12
0
 def test_multipolygon(self):
     g = geometry.MultiPoint([(0, 1), (0, 4)]).buffer(1.0)
     h = transform(lambda x, y, z=None: (x+1.0, y+1.0), g)
     self.assertEqual(h.geom_type, 'MultiPolygon')
     self.assertAlmostEqual(g.area, h.area)
     self.assertAlmostEqual(h.centroid.x, 1.0)
     self.assertAlmostEqual(h.centroid.y, 3.5)
Example #13
0
def area_from_lon_lat_poly(geometry):
    """
    Compute the area in km^2 of a shapely geometry, whose points are in longitude and latitude.

    Parameters
    ----------
    geometry: shapely geometry
        Points must be in longitude and latitude.

    Returns
    -------
    area:  float
        Area in km^2.

    """

    import pyproj
    from shapely.ops import transform
    from functools import partial


    project = partial(
        pyproj.transform,
        pyproj.Proj(init='epsg:4326'), # Source: Lon-Lat
        pyproj.Proj(proj='aea')) # Target: Albers Equal Area Conical https://en.wikipedia.org/wiki/Albers_projection

    new_geometry = transform(project, geometry)

    #default area is in m^2
    return new_geometry.area/1e6
def get_record_buffers(road_srid, records_csv_uuid):
    """Returns a list of all records, buffered by the configured MATCH_TOLERANCE
    :param road_srid: EPSG ID of roads shapefile projection
    :param records_csv_uuid: UUID of the BlackSpotRecordsFile CSV
    """
    road_projection = {'init': 'epsg:{}'.format(road_srid)}
    record_projection = {'init': RECORD_PROJECTION}
    project = partial(
        pyproj.transform,
        pyproj.Proj(record_projection),
        pyproj.Proj(road_projection)
    )
    record_buffers = []
    with BlackSpotRecordsFile.objects.get(uuid=records_csv_uuid).csv as records_csv:
        records_csv.open('rb')
        try:
            for row in csv.DictReader(records_csv):
                try:
                    record_point = transform(project, Point(float(row[RECORD_COL_X]),
                                                            float(row[RECORD_COL_Y])))
                    record_buffers.append(record_point.buffer(MATCH_TOLERANCE))
                except RuntimeError:
                    continue
        finally:
            records_csv.close()

    return record_buffers
Example #15
0
def getClassBalance(pshapes,bounds,proj):
    """
    Get native class balance of projected shapes, assuming a rectangular bounding box.
    :param pshapes:
      Sequence of projected shapely shapes
    :param bounds:
      Desired bounding box, in decimal degrees.
    :param proj:
      PyProj object defining orthographic projection of shapes.
    :returns:
      Float fraction of hazard polygons (area of hazard polygons/total area of bbox) 
    """
    xmin,ymin,xmax,ymax = bounds
    bpoly = Polygon([(xmin,ymax),
                     (xmax,ymax),
                     (xmax,ymin),
                     (xmin,ymin)])
    project = partial(
        pyproj.transform,
        pyproj.Proj(proj='latlong', datum='WGS84'),
        proj)
    bpolyproj = transform(project,bpoly)
    totalarea = bpolyproj.area
    polyarea = 0
    for pshape in pshapes:
        polyarea += pshape.area

    return polyarea/totalarea
Example #16
0
def project(geom, projection1, projection2):
    """Reproject a shapely geometry object to new coordinate system

    Parameters
    ----------
    geom: shapely geometry object
    projection1: string
        Proj4 string specifying source projection
    projection2: string
        Proj4 string specifying destination projection
    """
    projection1 = str(projection1)
    projection2 = str(projection2)


    # define projections
    pr1 = pyproj.Proj(projection1, errcheck=True, preserve_units=True)
    pr2 = pyproj.Proj(projection2, errcheck=True, preserve_units=True)

    # projection function
    # (see http://toblerity.org/shapely/shapely.html#module-shapely.ops)
    project = partial(pyproj.transform, pr1, pr2)

    # do the transformation!
    return transform(project, geom)
Example #17
0
def transform(g, from_srs, to_srs):
    project = partial(
        pyproj.transform,
        parse_projection(from_srs),
        parse_projection(to_srs))

    return ops.transform(project, g)
Example #18
0
    def to_crs(self, crs=None, epsg=None):
        """Transform geometries to a new coordinate reference system

        This method will transform all points in all objects.  It has
        no notion or projecting entire geometries.  All segments
        joining points are assumed to be lines in the current
        projection, not geodesics.  Objects crossing the dateline (or
        other projection boundary) will have undesirable behavior.
        """
        from fiona.crs import from_epsg
        if self.crs is None:
            raise ValueError('Cannot transform naive geometries.  '
                             'Please set a crs on the object first.')
        if crs is None:
            try:
                crs = from_epsg(epsg)
            except TypeError:
                raise TypeError('Must set either crs or epsg for output.')
        proj_in = pyproj.Proj(preserve_units=True, **self.crs)
        proj_out = pyproj.Proj(preserve_units=True, **crs)
        project = partial(pyproj.transform, proj_in, proj_out)
        result = self.apply(lambda geom: transform(project, geom))
        result.__class__ = GeoSeries
        result.crs = crs
        return result
def convert_coordinates(x, y, transformation):
    """"""

    src_pt = Point(x, y)
    dst_pt = ops.transform(transformation, src_pt)

    return dst_pt.x, dst_pt.y
Example #20
0
 def _get_mask(self, mask_type=None):
     assert mask_type
     exterior_str = str("eop:extentOf/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList")
     interior_str = str("eop:extentOf/gml:Polygon/gml:interior/gml:LinearRing/gml:posList")
     for item in self._metadata.iter("Pixel_Level_QI").next():
         if item.attrib.get("type") == mask_type:
             gml = os.path.join(self.granule_path, "QI_DATA/" + item.text)
     if self.dataset.is_zip:
         root = fromstring(self.dataset._zipfile.read(gml))
     else:
         root = parse(gml).getroot()
     nsmap = {k: v for k, v in root.nsmap.iteritems() if k}
     try:
         for mask_member in root.iterfind("eop:maskMembers", namespaces=nsmap):
             for feature in mask_member:
                 _type = feature.findtext("eop:maskType", namespaces=nsmap)
                 ext_pts = feature.find(exterior_str, nsmap).text.split()
                 exterior = _polygon_from_coords(ext_pts, fix_geom=True)
                 try:
                     interiors = [
                         _polygon_from_coords(int_pts.text.split(), fix_geom=True)
                         for int_pts in feature.findall(interior_str, nsmap)
                     ]
                 except AttributeError:
                     interiors = []
                 project = partial(pyproj.transform, pyproj.Proj(init=self.srid), pyproj.Proj(init="EPSG:4326"))
                 yield dict(
                     geometry=transform(project, Polygon(exterior, interiors)), attributes=dict(maskType=_type)
                 )
     except StopIteration:
         yield dict(geometry=Polygon(), attributes=dict(maskType=None))
         raise StopIteration()
Example #21
0
    def to_crs(self, crs=None, epsg=None):
        """Transform geometries to a new coordinate reference system

        This method will transform all points in all objects.  It has
        no notion or projecting entire geometries.  All segments
        joining points are assumed to be lines in the current
        projection, not geodesics.  Objects crossing the dateline (or
        other projection boundary) will have undesirable behavior.

        `to_crs` passes the `crs` argument to the `Proj` function from the
        `pyproj` library (with the option `preserve_units=True`). It can
        therefore accept proj4 projections in any format
        supported by `Proj`, including dictionaries, or proj4 strings.

        """
        from fiona.crs import from_epsg
        if self.crs is None:
            raise ValueError('Cannot transform naive geometries.  '
                             'Please set a crs on the object first.')
        if crs is None:
            try:
                crs = from_epsg(epsg)
            except TypeError:
                raise TypeError('Must set either crs or epsg for output.')
        proj_in = pyproj.Proj(self.crs, preserve_units=True)
        proj_out = pyproj.Proj(crs, preserve_units=True)
        project = partial(pyproj.transform, proj_in, proj_out)
        result = self.apply(lambda geom: transform(project, geom))
        result.__class__ = GeoSeries
        result.crs = crs
        result._invalidate_sindex()
        return result
Example #22
0
def projectdf(df, projection1, projection2):
    """Reproject a dataframe's geometry column to new coordinate system

    Parameters
    ----------
    df: dataframe
        Contains "geometry" column of shapely geometries

    projection1: string
        Proj4 string specifying source projection
    projection2: string
        Proj4 string specifying destination projection
    """
    projection1 = str(projection1)
    projection2 = str(projection2)


    # define projections
    pr1 = pyproj.Proj(projection1, errcheck=True, preserve_units=True)
    pr2 = pyproj.Proj(projection2, errcheck=True, preserve_units=True)

    # projection function
    # (see http://toblerity.org/shapely/shapely.html#module-shapely.ops)
    project = partial(pyproj.transform, pr1, pr2)

    # do the transformation!
    newgeo = [transform(project, g) for g in df.geometry]

    return newgeo
Example #23
0
 def test_polygon(self):
     g = geometry.Point(0, 1).buffer(1.0)
     h = transform(lambda x, y, z=None: (x+1.0, y+1.0), g)
     self.assertEqual(h.geom_type, 'Polygon')
     self.assertAlmostEqual(g.area, h.area)
     self.assertAlmostEqual(h.centroid.x, 1.0)
     self.assertAlmostEqual(h.centroid.y, 2.0)
Example #24
0
    def window_at(self, geom, window_shape):
        """Return a subsetted window of a given size, centered on a geometry object

        Useful for generating training sets from vector training data
        Will throw a ValueError if the window is not within the image bounds

        Args:
            geom (shapely,geometry): Geometry to center the image on
            window_shape (tuple): The desired shape of the image as (height, width) in pixels.

        Returns:
            image: image object of same type
        """
        # Centroids of the input geometry may not be centered on the object.
        # For a covering image we use the bounds instead.
        # This is also a workaround for issue 387.
        y_size, x_size = window_shape[0], window_shape[1]
        bounds = box(*geom.bounds)
        px = ops.transform(self.__geo_transform__.rev, bounds).centroid
        miny, maxy = int(px.y - y_size/2), int(px.y + y_size/2)
        minx, maxx = int(px.x - x_size/2), int(px.x + x_size/2)
        _, y_max, x_max = self.shape
        if minx < 0 or miny < 0 or maxx > x_max or maxy > y_max:
            raise ValueError("Input geometry resulted in a window outside of the image")
        return self[:, miny:maxy, minx:maxx]
Example #25
0
 def _reproject(self, geometry, from_proj=None, to_proj=None):
     if from_proj is None:
         from_proj = self._default_proj
     if to_proj is None:
         to_proj = self.proj if self.proj is not None else "EPSG:4326"
     tfm = partial(pyproj.transform, get_proj(from_proj), get_proj(to_proj))
     return ops.transform(tfm, geometry)
def geo_geodetic_to_cartesian(geo_wkt):
    """
    Transforms a wkt string of geodetic coordinates to cartesian coordinates
    :param geo_wkt: wkt string of geodetic coordinates
    :returns : wkt string object of cartesian coordinates
    - - - Transformation information - - -
    'EPSG:4326': LatLon with WGS84 datum used by GPS units and Google Earth
    'aea': 'Albers Equal Area is a conic, equal area map projection that uses
    two standard parallels. Although scale and shape are not preserved,
    distortion is minimal between the standard parallels.
    """

    geo = loads(geo_wkt)
    lon_vec = geo.xy[0]
    lat_vec = geo.xy[1]
    geo_transformed = transform(
        partial(pyproj.transform,
                pyproj.Proj(init='EPSG:4326'),
                pyproj.Proj(proj='aea',
                            lon=lon_vec,
                            lat=lat_vec)
            ),
        geo)

    return geo_transformed.wkt
def load_tiger_data():
    """"""

    transformation = check_epsg_for_transformation()

    # if the foreign key flag is set to true reflect geoheader tables
    # in matching acs schemas, tiger data is matched to acs that is one
    # year less recent because it is released one year sooner
    if gv.foreign_key:
        acs_year = gv.tiger_year - 1
        for i in ACS_SPANS:
            # FIXME should probably template this on global level
            acs_schema = ACS_SCHEMA.format(yr=acs_year, span=i)
            try:
                gv.metadata.reflect(schema=acs_schema, only=[GEOHEADER])
            except sqlalchemy.exc.InvalidRequestError:
                pass

    for shp_path, product in gv.shp.items():
        with fiona.open(shp_path) as tiger_shape:
            shp_metadata = tiger_shape.meta.copy()
            table = create_tiger_table(shp_metadata, product)

            print '\nloading shapefile "{0}" ' \
                  'into table: "{1}.{2}":'.format(
                       basename(shp_path), gv.metadata.schema, table.name)
            print 'features inserted:'

            memory_tbl = list()
            max_fid = max(tiger_shape.keys())
            for fid, feat in tiger_shape.items():
                fields = feat['properties']
                row = {k.lower(): v for k, v in fields.items()}

                # casting to multipolygon here because a few features
                # are multi's and the geometry types must match
                shapely_geom = MultiPolygon([shape(feat['geometry'])])

                if transformation:
                    shapely_geom = ops.transform(transformation, shapely_geom)

                # geoalchemy2 requires that geometry be in EWKT format
                # for inserts, that conversion is made below
                ga2_geom = WKTElement(shapely_geom.wkt, gv.epsg)
                row['geom'] = ga2_geom
                memory_tbl.append(row)

                count = fid + 1
                if count % 1000 == 0 or fid == max_fid:
                    gv.engine.execute(table.insert(), memory_tbl)
                    memory_tbl = list()

                    # logging to inform the user
                    if count % 20000 == 0:
                        sys.stdout.write(str(count))
                    elif fid == max_fid:
                        print '\n'
                    else:
                        sys.stdout.write('..')
Example #28
0
    def distances(self, shp, proj=None):
        """Compute the euclidean distances for each of the shapes in the vector
        layer. If proj is not none, it will transform shp into proj.

        Note: if shp is a shapely object, it is upto to the user
        to make sure shp is in the correct coordinate system.

        Parameters
        ----------
        proj: string or osr.SpatialReference (default=None)
            valid strings are 'albers' or 'utm'. If None, no
            transformation of coordinates.

        Returns
        -------
        pandas.Series


        Note
        ----
        'utm' should only be used for small polygons when centimeter
        level accuraccy is needed.  Othewise the area will
        be incorrect.  Similar issues can happen when polygons cross
        utm boundaries.
        """
        if proj is None:
            shp = to_geometry(shp)
            return self.to_geometry(proj=proj).map(lambda x: x.Distance(shp))

        if proj == 'utm':
            if not self.proj.ExportToProj4() == ut.PROJ_WGS84:
                vl = self.transform(ut.projection_from_string())
            else:
                vl = self

            _shp = ops.transform(to_utm, to_shapely(shp))
            d = vl.to_shapely() \
                  .map(lambda x: ops.transform(to_utm, x).distance(_shp))
            s = pd.Series(d, index=self.index)
            return s

        elif proj == 'albers':
            proj = ut.projection_from_string(ut.ALBERS_N_AMERICA)

        shp = to_geometry(shp, copy=True, proj=proj)
        return self.to_geometry(proj=proj).map(lambda x: x.Distance(shp))
Example #29
0
        def make_patch(shape, row):

            args = dict(kwargs.items())

            if args_f:
                args.update(args_f(row))

            return PolygonPatch(transform(map_xform, shape), **args)
Example #30
0
 def _transform(self, qgs_geometry, point_transformation):
     """returns a transformed geometry"""
     #@todo use wkb to optimize ?
     geom = loads(qgs_geometry.exportToWkt().replace("Z", " Z"))
     return QgsGeometry.fromWkt(
             transform(
                 lambda x,y,z: point_transformation(x, y, z),
                 geom).wkt)
def convert_crs_point(point_x, point_y, in_proj, out_proj):
    """
    convert_crs_point reprojects a point into a different coordinate system
    :param point_x: latitude value of the point
    :param point_y: longitude value of the point
    :param in_proj: input coordinate system
    :param out_proj: output coordinate system
    :returns:
        - AoI_point - point object in the new coordinate system
    """
    in_pt = Point(point_x, point_y)
    #wgs84_pt = points_gdf['geometry'][i]
    # reproject point
    in_proj = pyproj.CRS(in_proj)
    out_proj = pyproj.CRS(out_proj)
    project = pyproj.Transformer.from_crs(in_proj, out_proj,
                                          always_xy=True).transform
    AoI_point = transform(project, in_pt)
    return AoI_point
Example #32
0
    def to_crs(self, crs=None, epsg=None):
        """Returns a ``GeoSeries`` with all geometries transformed to a new
        coordinate reference system.

        Transform all geometries in a GeoSeries to a different coordinate
        reference system.  The ``crs`` attribute on the current GeoSeries must
        be set.  Either ``crs`` in string or dictionary form or an EPSG code
        may be specified for output.

        This method will transform all points in all objects.  It has no notion
        or projecting entire geometries.  All segments joining points are
        assumed to be lines in the current projection, not geodesics.  Objects
        crossing the dateline (or other projection boundary) will have
        undesirable behavior.

        Parameters
        ----------
        crs : dict or str
            Output projection parameters as string or in dictionary form.
        epsg : int
            EPSG code specifying output projection.
        """
        from fiona.crs import from_epsg
        if self.crs is None:
            raise ValueError('Cannot transform naive geometries.  '
                             'Please set a crs on the object first.')
        if crs is None:
            try:
                crs = from_epsg(epsg)
            except TypeError:
                raise TypeError('Must set either crs or epsg for output.')
        proj_in = pyproj.Proj(self.crs, preserve_units=True)
        proj_out = pyproj.Proj(crs, preserve_units=True)
        if _PYPROJ2:
            transformer = pyproj.Transformer.from_proj(proj_in, proj_out)
            project = transformer.transform
        else:
            project = partial(pyproj.transform, proj_in, proj_out)
        result = self.apply(lambda geom: transform(project, geom))
        result.__class__ = GeoSeries
        result.crs = crs
        result._invalidate_sindex()
        return result
Example #33
0
def transform(data, func):
    if compat.USE_PYGEOS:
        coords = pygeos.get_coordinates(data)
        new_coords = func(coords[:, 0], coords[:, 1])
        result = pygeos.set_coordinates(data.copy(), np.array(new_coords).T)
        return result
    else:
        from shapely.ops import transform

        n = len(data)
        result = np.empty(n, dtype=object)
        for i in range(n):
            geom = data[i]
            if isna(geom):
                result[i] = geom
            else:
                result[i] = transform(func, geom)

        return result
def convert_shape_to_projected_crs(line, original_crs, new_crs):
    """
    Existing elevation path needs to be converted from WGS84 to projected
    coordinates.

    """
    # Geometry transform function based on pyproj.transform
    project = partial(pyproj.transform, pyproj.Proj(init=original_crs),
                      pyproj.Proj(init=new_crs))

    new_geom = transform(project, LineString(line['geometry']['coordinates']))

    output = {
        'type': 'Feature',
        'geometry': mapping(new_geom),
        'properties': line['properties']
    }

    return output
Example #35
0
def polygon_to_placekeys(poly, include_touching=False, geo_json=False):
    """
    Given a shapely Polygon, return Placekeys contained in
    or intersecting the boundary of the polygon.

    :param poly: shapely Polygon object
    :param include_touching: If True Placekeys whose hexagon boundary only touches
        that of the input polygon are included in the set of boundary Placekeys.
        Default is False.
    :param geo_json: If True assume coordinates in `poly` are in GeoJSON format:
        (long, lat)-tuples and with the first and last tuples identical, and in
        counter-clockwise orientation. If False (default) assumes tuples will be
        (lat, long).
    :return: A dictionary with keys 'interior' and 'boundary' whose values are
        tuples of Placekeys that are contained in poly or which intersect the
        boundary of poly respectively.

    """
    if geo_json:
        poly = transform(lambda x, y: (y, x), poly)

    buffer_size = 2e-3
    buffered_poly = poly.buffer(buffer_size)
    candidate_hexes = h3_int.polyfill(mapping(buffered_poly), 10)

    tree = STRtree([poly])
    interior_hexes = []
    boundary_hexes = []
    for h in list(candidate_hexes):
        hex_poly = Polygon(h3_int.h3_to_geo_boundary(h))
        if len(tree.query(hex_poly)) > 0:
            if poly.contains(hex_poly):
                interior_hexes.append(h)
            elif poly.intersects(hex_poly):
                if include_touching:
                    boundary_hexes.append(h)
                elif not include_touching and not poly.touches(hex_poly):
                    boundary_hexes.append(h)

    return {
        'interior': tuple(h3_int_to_placekey(h) for h in interior_hexes),
        'boundary': tuple(h3_int_to_placekey(h) for h in boundary_hexes)
    }
Example #36
0
def get_shape_filter(shapefile):
    """
    Return shapefile filter. All the geoprocessing is done only once for that
    reason we are using a generator function.
    """
    with fiona.open(shapefile) as collection:
        shp = collection[0]['geometry']
        project = partial(
            pyproj.transform,
            pyproj.Proj(init=collection.crs['init']),
            pyproj.Proj(init='epsg:4326'))
        shp = transform(project, shape(shp))

    def filter_function(item):
        if item['properties'].get('available'):
            return True
        return shp.intersects(shape(item['geometry']))

    return filter_function
Example #37
0
def untrans(p, epsg_code):
    point1 = Point(p[:2])
    # print 'point1: ', point1
    project = partial(
        pyproj.transform,
        # epsg:7405 is suitable for london lidar
        pyproj.Proj(init=epsg_code),
        # epsg:32633 is suitable for berlin citygml
        # pyproj.Proj(init='epsg:32633'),
        # epsg:3068 is suitable for pariser platz citygml
        # pyproj.Proj(init='epsg:3068'),  # source coordinate system
        pyproj.Proj(init='epsg:4326'))  # destination coordinate system

    point2 = transform(project, point1)
    # print (point2.x)
    res = [point2.x, point2.y]
    # print 'res: ', res
    # res.extend(p[2:])
    return res
Example #38
0
    def get_distance_to_closest_tertiary_road(self, lat, lon):
        '''
        Returns the distance in metres from the nearest tertiary road  - 1km radius
         i.e.  closest_tertiary_distance,
        '''
        try:

            G = ox.graph_from_point((lat, lon),
                                    dist=1000,
                                    network_type='drive')
            gdf = ox.graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
            gdf.to_crs(epsg=3310, inplace=True)
            gdf = gdf.reset_index(
            )  ## to ensure the values of u & v are in the columns

            # convert point to utm in order to get distance in metres
            wgs84_pt = Point(lon, lat)
            wgs84 = pyproj.CRS('EPSG:4326')
            utm = pyproj.CRS('EPSG:3310')
            project = pyproj.Transformer.from_crs(wgs84, utm,
                                                  always_xy=True).transform
            utm_point = transform(project, wgs84_pt)

            tertiary_gdf = gdf[gdf['highway'] == 'tertiary']
            if tertiary_gdf.shape[0] == 0:
                closest_tertiary_distance = None
            else:
                tertiary_roads = tertiary_gdf[['geometry', 'u',
                                               'v']].values.tolist()
                tertiary_roads_with_distances = [(road,
                                                  utm_point.distance(road[0]))
                                                 for road in tertiary_roads]
                tertiary_roads_with_distances = sorted(
                    tertiary_roads_with_distances, key=lambda x: x[1])
                closest_tertiary_road = tertiary_roads_with_distances[0]
                closest_tertiary_distance = round(closest_tertiary_road[1], 2)

        except Exception as ex:
            closest_tertiary_distance = None
            print(ex)

        return closest_tertiary_distance
Example #39
0
def main():
    with open(lk_geojson_p) as f:
        lk = geojson.load(f)
    features = lk['features']

    dict_list = []
    for feature in tqdm(features):
        prop, geom = feature["properties"], feature["geometry"]

        poly = shape(geom)
        lon, lat = poly.centroid.coords[0]

        # project to EPSG:5243 (ETRS89) for correct area calculation
        proj = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'),
                       pyproj.Proj(init='epsg:5243'))
        poly_projected = transform(proj, poly)
        x_epsg5243, y_epsg5243 = poly_projected.centroid.coords[0]
        projected_area = poly_projected.area
        try:
            dict_list.append({
                "name": prop["name_2"],
                "hasc_2": prop["hasc_2"],
                "krs": prop["cca_2"],
                "bundesland": prop["name_1"],
                "centr_lon": lon,
                "centr_lat": lat,
                "x_epsg5243": x_epsg5243,
                "y_epsg5243": y_epsg5243,
                "total_area_epsg5243": projected_area
            })
        except KeyError:
            print(f"Incomplete Row was skipped: \n{prop}")
            continue

    df_lks = pd.DataFrame(dict_list).set_index(
        "krs").sort_index().reset_index()

    # Fix error in lk data set for Göttingen. ID should be 03159
    idx = df_lks["krs"] == "03152"
    df_lks.loc[idx, "krs"] = "03159"
    df_lks.to_csv(proj_dir / "data/02_pre_processed/landkreis_areas.csv",
                  encoding='utf-8')
Example #40
0
def get_naip_scenes():

    naip_paths = glob.glob(os.path.join(NAIP_DIR, "m_*tif"))
    print(f"found {len(naip_paths)} naip scenes")

    naip_scenes = {}

    for naip_path in naip_paths:

        naip_file = os.path.split(naip_path)[1]
        building_outpath = os.path.join(
            BUILDING_ANNOTATION_DIR, naip_file.replace(".tif", ".shp")
        )
        if os.path.exists(building_outpath):
            print(
                f"{building_outpath} already exists, skipping corresponding naip scene"
            )
            continue

        naip = rasterio.open(naip_path)

        projection_fn = partial(
            pyproj.transform, pyproj.Proj(naip.crs), pyproj.Proj("epsg:4326")
        )

        # TODO Is rasterio bounds convention different from shapely's (xy order switched?)
        minx, miny, maxx, maxy = naip.bounds

        bbox_shape = Polygon([[minx, miny], [maxx, miny], [maxx, maxy], [minx, maxy]])

        # Note: projected_bbox_shape.exterior.coords.xy are in reverse order (y then x)  # TODO Why?
        projected_bbox_shape = transform(projection_fn, bbox_shape)

        minlat, minlon, maxlat, maxlon = projected_bbox_shape.bounds

        bbox_shape_lonlat = Polygon(
            [[minlon, minlat], [maxlon, minlat], [maxlon, maxlat], [minlon, maxlat]]
        )

        naip_scenes[naip_path] = {"buildings": [], "bbox_lonlat": bbox_shape_lonlat}

    return naip_scenes
Example #41
0
def generate_geojson_from_coords_df(df, kbuf):
    pll = df[['lat', 'long']]
    geometryX = [Point(xy) for xy in zip(pll['long'], pll['lat'])]

    gjlist = []
    lbuf = 0.01
    xbuf = lbuf  #/2.0
    mn = 0
    totalm2 = 0.0
    for pp in geometryX:
        listarray = []
        #listarray.append([pp.x, pp.y])
        listarray.append([pp.x + xbuf, pp.y - lbuf])
        listarray.append([pp.x - xbuf, pp.y - lbuf])
        listarray.append([pp.x - xbuf, pp.y + lbuf])
        listarray.append([pp.x + xbuf, pp.y + lbuf])  # self intersection?

        nparray = np.array(listarray)
        poly = geometry.Polygon(nparray)

        geom = poly
        geom_area = ops.transform(
            partial(
                pyproj.transform, pyproj.Proj(init='EPSG:4326'),
                pyproj.Proj(proj='aea',
                            lat1=geom.bounds[1],
                            lat2=geom.bounds[3])), geom)

        # Print the area in m^2
        #print(geom_area.area)
        totalm2 += geom_area.area
        convex_hull_x, convex_hull_y = [
            z.tolist() for z in poly.convex_hull.exterior.coords.xy
        ]
        #print(poly.wkt)
        g1 = shapely.geometry.mapping(poly)
        gjlist.append(json.dumps(g1))
        #print(json.dumps(g1,indent=1))
        #print(mn)
        mn += 1
    #print("Generated geojson estimated to consume ",totalm2/1000000.0," square kilometers per instance")
    return (gjlist)
Example #42
0
    def project_shape(
        self, projection: Union[pyproj.Proj, crs.Projection, None] = None
    ) -> base.BaseGeometry:
        """Returns a projected representation of the shape.

        By default, an equivalent projection is applied. Equivalent projections
        locally respect areas, which is convenient for the area attribute.

        Other valid projections are available:

        - as ``pyproj.Proj`` objects;
        - as ``cartopy.crs.Projection`` objects.

        """

        if self.shape is None:
            return None

        if isinstance(projection, crs.Projection):
            projection = pyproj.Proj(projection.proj4_init)

        if projection is None:
            bounds = self.bounds
            projection = pyproj.Proj(
                proj="aea",  # equivalent projection
                lat_1=bounds[1],
                lat_2=bounds[3],
                lat_0=(bounds[1] + bounds[3]) / 2,
                lon_0=(bounds[0] + bounds[2]) / 2,
            )

        transformer = pyproj.Transformer.from_proj(
            pyproj.Proj("epsg:4326"), projection, always_xy=True
        )
        projected_shape = transform(
            transformer.transform,
            self.shape,
        )

        if not projected_shape.is_valid:
            warnings.warn("The chosen projection is invalid for current shape")
        return projected_shape
Example #43
0
    def window_cover(self, window_shape, pad=True):
        """ Iterate over a grid of windows of a specified shape covering an image.

        The image is divided into a grid of tiles of size window_shape. Each iteration returns
        the next window.
        

        Args:
            window_shape (tuple): The desired shape of each image as (height,
                width) in pixels.
            pad: (bool): Whether or not to pad edge cells. If False, cells that do not
                have the desired shape will not be returned. Defaults to True.

        Yields:
            image: image object of same type.
        """
        size_y, size_x = window_shape[0], window_shape[1]
        _ndepth, _nheight, _nwidth = self.shape
        nheight, _m = divmod(_nheight, size_y)
        nwidth, _n = divmod(_nwidth, size_x)

        img = self
        if pad is True:
            new_height, new_width = _nheight, _nwidth
            if _m != 0:
                new_height = (nheight + 1) * size_y
            if _n != 0:
                new_width = (nwidth + 1) * size_x
            if (new_height, new_width) != (_nheight, _nwidth):
                bounds = box(0, 0, new_width, new_height)
                geom = ops.transform(self.__geo_transform__.fwd, bounds)
                img = self[geom]

        row_lims = range(0, img.shape[1], size_y)
        col_lims = range(0, img.shape[2], size_x)
        for maxy, maxx in product(row_lims, col_lims):
            reg = img[:, maxy:(maxy + size_y), maxx:(maxx + size_x)]
            if pad is False:
                if reg.shape[1:] == window_shape:
                    yield reg
            else:
                yield reg
def ways2geometry(overpass_result_object):
    """Takes an OverpassResult object as returned by the OSMPythonTools query function.\n
    Converts OverpassResult result object into a Pandas Geodataframe with initialised geometry attribute. Also returns dict with OSM tags for each way.\n
    Currently only supports queries for OSM ways (e.g. elementType=['way'])."""
    tunnel_json = overpass_result_object.toJSON()
    # Read ['elements'] attribute into a df:
    df = pd.DataFrame.from_records(tunnel_json['elements'])
    df.rename(columns={'nodes': 'node_IDs'}, inplace=True)
    # Clean up the geometry column which contains the coordinates, but has 'lat', 'lon' strings etc.
    df['geometry'] = df['geometry'].astype(str)
    df['geometry'].replace({
        r"{'lat': ": r'(',
        r"'lon': ": '',
        r'}': r')'
    },
                           inplace=True,
                           regex=True)
    # Convert string representations into a list of tuples of floats.
    df['geometry'] = [literal_eval(row) for row in df['geometry']]
    if not isinstance(df.geometry[1][1], tuple):
        raise ValueError(
            "Geometry coordinates must be of <class 'tuple'>. Conversion failed."
        )

    # Unpack the 'tags' into a dictionary. This way we avoid NaNs and just have unique dict for every way ID key.
    way_tags = {}
    for way in df[['id', 'tags']].itertuples():
        way_tags[way.id] = way.tags
    # Finally delete the 'tags' col (no longer needed). axis=1 specifies column, not row.
    df.drop(columns='tags', axis=1, inplace=True)

    # Construct a Geopandas gdf and enable the 'geometry' column.
    gdf = gpd.GeoDataFrame(
        df,
        geometry=df['geometry'].apply(lambda row: LineString(row)),
        crs='epsg:4326')  # EPSG: 4326 is WGS84 (Lat and Long)
    # Flip the LineString coords as they are the wrong way around.
    gdf['geometry'] = gdf.geometry.map(
        lambda linestring: transform(lambda x, y: (y, x), linestring))
    gdf.set_crs(epsg='4326', inplace=True)  # Set lon lat system again.

    return gdf, way_tags
Example #45
0
def getPloyandArea(contours, zoom, lat, lng):
    res = {}
    box = [[[0, 0], [0, 640], [640, 640], [640, 0], [0, 0]]]
    aarea = getArea(box, zoom, lat, lng)

    for k in contours:
        del_li, matrix = getMatrix(contours[k])
        poly_outer = []
        poly_inner = []
        area_list = []

        hole_list = []
        for m in range(len(matrix)):
            foo_index = [n for n, x in enumerate(matrix[m]) if x == 1]
            hole_list += foo_index

        for x in range(len(contours[k])):
            if x not in del_li and x not in hole_list:
                index = np.where(np.array(matrix[x]) == 1)
                outer = getCoords([contours[k][x]], zoom, lat, lng)[0]
                inner = getCoords(np.array(contours[k])[index], zoom, lat, lng)
                myPloy = Polygon(outer, inner)

                geom_area = ops.transform(
                    partial(
                        pyproj.transform, pyproj.Proj(init='EPSG:4326'),
                        pyproj.Proj(proj='aea',
                                    lat1=myPloy.bounds[1],
                                    lat2=myPloy.bounds[3])), myPloy)
                if int(geom_area.area) != 0:
                    poly_outer.append(outer)
                    poly_inner.append(inner)
                    area_list.append(int(geom_area.area))

        res[k] = {
            'gdata': getGeojson(poly_outer, poly_inner, area_list),
            'number': len(area_list),
            'area': sum(area_list),
            'percentage': int(sum(area_list) * 1.0 / aarea * 100)
        }

    return res
Example #46
0
    def wrapper(self, *args, **kwargs):
        result = method(self, *args, **kwargs)
        if isinstance(result, tuple):
            *other, geom = result
        else:
            other = []
            geom = result
        if self.normalize:

            def d_trasf(x, y):
                x_out = x * self.scale[0] + self.center[0]
                y_out = y * self.scale[1] + self.center[1]
                return x_out, y_out

            geom = transform(d_trasf, geom)

        if other:
            return (*other, geom)
        else:
            return geom
def extract_feature(feature, dem, embed=False):
    geom = shape(feature['geometry'])
    # QGIS uses a negative Y axis for pixel coordinates
    tfunc = lambda x, y: (x, -y)
    geom = transform(tfunc, geom)

    # We don't support multiband extraction yet
    x = extract_shape(geom, dem, band=1)
    y = extract_shape(geom, dem, band=2)
    z = extract_shape(geom, dem, band=3)

    # Z in Curiosity site frame is apparently down, whose idea was that?
    z *= -1

    ix = N.index_exp[:, -1]
    xyz = N.column_stack((x[ix], y[ix], z[ix]))

    xyz = xyz[xyz.sum(axis=1) != 0]
    # We don't deal well with doubles right now
    return xyz.astype(N.float32)
Example #48
0
def geo_to_utm(shape, utmstr=None):
    """Project a shapely geometry to UTM.

    Args:
        shape (shapely.geometry.shape): Shapely geometry (Polygon, LineString, etc.)
        utmstr (str): Proj4 projection string with appropriate UTM projection. If None,
            geo_to_utm will select one based on shape centroid.
    Returns:
        shapely.geometry.shape: Input shapely geometry projected to UTM.
        str: Proj4 projection string with UTM projection used.
    """
    if utmstr is None:
        point = shape.centroid
        utmstr = get_utm_proj(point.y, point.x)
    project = partial(pyproj.transform,
                      pyproj.Proj('+proj=longlat +datum=WGS84'),
                      pyproj.Proj(utmstr))

    pshape = transform(project, shape)
    return (pshape, utmstr)
Example #49
0
def write_geojson(shapes_and_probs, output_path):
    import json
    from shapely.geometry import mapping
    d = {'type': 'FeatureCollection', 'features': []}
    for shape, prob in shapes_and_probs:
        project = partial(
            pyproj.transform,
            pyproj.Proj(init='epsg:3857'),
            pyproj.Proj(init='epsg:4326'))
        shape_wgs = transform(project, shape)
        feat = {
            'type': 'Feature',
            'geometry': mapping(shape_wgs),
            'properties': {
                'prob': prob
            }
        }
        d['features'].append(feat)
    with open(output_path, 'w') as f:
        f.write(json.dumps(d))
Example #50
0
def get_area_acres(geometry):
    """ Calculate area in acres for a GeoJSON geometry
    :param geometry: A GeoJSON Polygon geometry
    :returns: Area in acres
    """

    shapely_geometry = shape(geometry)
    geom_aea = transform(
        partial(
            pyproj.transform,
            pyproj.Proj(init="EPSG:4326"),
            pyproj.Proj(
                proj="aea",
                lat1=shapely_geometry.bounds[1],
                lat2=shapely_geometry.bounds[3],
            ),
        ),
        shapely_geometry,
    )
    return round(geom_aea.area / 4046.8564224)
Example #51
0
    def __geodesic_point_buffer(self, lon, lat, km):
        """
        Based on: https://gis.stackexchange.com/questions/289044/creating-buffer-circle-x-kilometers-from-point-using-python

        Args:
            lon:
            lat:
            km:

        Returns:
            a list of coordinates with radius km and center (lat,long) in this projection
        """
        proj_wgs84 = pyproj.Proj(init='epsg:4326')
        # Azimuthal equidistant projection
        aeqd_proj = '+proj=aeqd +lat_0={lat} +lon_0={lon} +x_0=0 +y_0=0'
        project = partial(pyproj.transform,
                          pyproj.Proj(aeqd_proj.format(lat=lat, lon=lon)),
                          proj_wgs84)
        buf = Point(0, 0).buffer(km * 1000)  # distance in metres
        return transform(project, buf).exterior.coords[:]
Example #52
0
def load_USA_geojson(us_geojson_file):
    '''
    Load USA main land geojson,
    and project it to epsg:2163 projection coordinate system
    '''
    # the data we get from https://raw.githubusercontent.com/johan/world.geo.json/master/countries/USA.geo.json
    # us_geojson_file = "../geokg_collect/dbgeo_code/input/USA.geojson"

    # us_gdf = gpd.read_file(us_geojson_file)
    us_gdf = explode(us_geojson_file)

    project = partial(
        pyproj.transform,
        pyproj.Proj(init='epsg:4326'),  # source coordinate system
        pyproj.Proj(init='epsg:2163'))  # destination coordinate system

    polygon = us_gdf.loc[5, 'geometry']
    polygon_proj_raw = transform(project, polygon)

    return project, polygon, polygon_proj_raw
Example #53
0
    def download_masked_reproject(self, tmp_path, format):
        input = self.create_spacetime_layer()
        imagecollection = GeopysparkDataCube(pyramid=gps.Pyramid({0: input}))
        imagecollection.metadata=imagecollection.metadata.add_dimension('band_one', 'band_one', 'bands')
        imagecollection.metadata=imagecollection.metadata.append_band(Band('band_two','',''))

        polygon = geometry.Polygon([[0, 0], [1.9, 0], [1.9, 1.9], [0, 1.9]])
        import pyproj
        from shapely.ops import transform
        from functools import partial
        project = partial(
            pyproj.transform,
            pyproj.Proj(init="EPSG:4326"),  # source coordinate system
            pyproj.Proj(init="EPSG:3857"))  # destination coordinate system
        reprojected = transform(project, polygon)
        imagecollection = imagecollection.mask_polygon(mask=reprojected, srs="EPSG:3857")

        filename = str(tmp_path / "test_download_masked_result.3857.") + format
        res = imagecollection.save_result(filename, format=format)
        print(res)
Example #54
0
def _poly_area_approximation(way, nodes):
    """ Compute the approximated area of an irregular OSM polygon.
        see: https://arachnoid.com/area_irregular_polygon/
             https://gist.github.com/robinkraft/c6de2f988c9d3f01af3c
    """
    points = []
    for node in way['nd']:
        points.append([
            float(nodes[node['ref']]['lat']),
            float(nodes[node['ref']]['lon'])
        ])

    approx = MultiPoint(points).convex_hull
    # http://openstreetmapdata.com/info/projections
    proj = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'),
                   pyproj.Proj(init='epsg:3857'))

    converted_approximation = transform(proj, approx)

    return converted_approximation.area
Example #55
0
def polygon_projection(poly, from_epsg, to_epsg):
    """
    Projects a polygon from an initial epsg code to another

    :param poly: poly to project
    :type poly: Polygon
    :param from_epsg: initial epsg code
    :type from_epsg: int
    :param to_epsg: final epsg code
    :type to_epsg: int
    :return: The polygon in the final projection
    :rtype: Polygon
    """
    project = pyproj.Transformer.from_proj(
        pyproj.Proj(
            init='epsg:{}'.format(from_epsg)), pyproj.Proj(
            init='epsg:{}'.format(to_epsg)))
    poly = transform(project.transform, poly)

    return poly
Example #56
0
async def get_gis_polygon(polygon_id: int):
    """
    Эндпоинт для получения записи gis_polygon по id
    :param polygon_id: id записи
    :return: Сериализованная запись
    """
    query = db.gis_polygon.select().where(db.gis_polygon.c.id == polygon_id)
    try:
        result = await db.database.fetch_one(query)
    except Exception as exc:
        logging.log(logging.ERROR, exc)
        raise exc
    if not result:
        raise HTTPException(status_code=404, detail="GIS Polygon not found")
    ewkt_geom = to_shape(result.get('geometry'))
    transformer = Transformer.from_proj(Proj('epsg:4326'), Proj('epsg:32644'))
    geometry = transform(transformer.transform, ewkt_geom)
    result = dict(result)
    result.update({'geometry': str(geometry)[10:-2]})
    return result
Example #57
0
def geodesic_point_buffer(lat: float, lng: float, zoom: int) -> geoPolygon:
    """Return either a point at or polygon surrounding provided latitude and
    longitude (depending on zoom level) as geoJSON see
    https://gis.stackexchange.com/questions/289044/creating-buffer-circle-x-
    kilometers-from-point-using-python."""
    buffer_distance: float = _get_buffer_distance(zoom)

    proj_wgs84 = pyproj.Proj(init="epsg:4326")
    # Azimuthal equidistant projection
    aeqd_proj = "+proj=aeqd +lat_0={lat} +lon_0={lon} +x_0=0 +y_0=0"
    project = partial(
        pyproj.transform,
        pyproj.Proj(aeqd_proj.format(lat=lat, lon=lng)),
        proj_wgs84,
    )
    buf: int = Point(0, 0).buffer(buffer_distance)  # distance in metres

    coord_list = transform(project, buf).exterior.coords[:]

    return geoPolygon([coord_list])
def reproject(geom, to_srs='epsg:5070', from_srs='epsg:4326'):
    """"
    Reproject `geom` from one spatial ref to another
    Args:
        geom (Shapely Geometry): A geometry object with coordinates to
            transform.
        from_srs (string): An EPSG code in the format of `epsg:nnnn` that
            specifies the existing spatial ref for `geom`
        to_srs (string): An EPSG code in the format of `epsg:nnnn` that
            specifies the desired ref for the returned geometry
    Returns:
        Shapely Geometry with coordinates transformed to the desired srs
    """
    projection = partial(
        pyproj.transform,
        pyproj.Proj(init=from_srs),
        pyproj.Proj(init=to_srs),
    )

    return transform(projection, geom)
Example #59
0
def _get_midpoints(highValueArea):
    """get midpoints from polygon and multipolygon shapes for current CLIMADA-
    exposure compatibility (centroids / points)

    Parameters:
        highValueArea (gdf)

    Returns:
        High_Value_Area_gdf
    """
    High_Value_Area_gdf = geopandas.read_file(highValueArea)

    # For current exposure structure, simply get centroid
    # and area (in m2) for each building polygon
    High_Value_Area_gdf['projected_area'] = 0
    High_Value_Area_gdf['Midpoint'] = 0

    for index in High_Value_Area_gdf.index:
        High_Value_Area_gdf.loc[index, "Midpoint"] = \
        High_Value_Area_gdf.loc[index, "geometry"].centroid.wkt
        s = shape(High_Value_Area_gdf.loc[index, "geometry"])
        # turn warnings off, otherwise Future and Deprecation warnings are flooding the logs
        logging.captureWarnings(True)
        proj = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'),
                       pyproj.Proj(init='epsg:3857'))
        High_Value_Area_gdf.loc[index, "projected_area"] = transform(proj,
                                                                     s).area
        # turn warnings on again
        logging.captureWarnings(False)

    # change active geometry from polygons to midpoints
    from shapely.wkt import loads
    High_Value_Area_gdf = High_Value_Area_gdf.rename(columns={
        'geometry': 'geo_polys',
        'Midpoint': 'geometry'
    })
    High_Value_Area_gdf['geometry'] = High_Value_Area_gdf['geometry'].apply(
        lambda x: loads(x))
    High_Value_Area_gdf = High_Value_Area_gdf.set_geometry('geometry')

    return High_Value_Area_gdf
Example #60
0
    def areas(self, proj=None):
        """Compute the areas for each of the shapes in the vector
        layer.

        Parameters
        ----------
        proj: string or osr.SpatialReference (default=None)
            valid strings are 'albers' or 'utm'. If None, no
            transformation of coordinates.

        Returns
        -------
        pandas.Series


        Note
        ----
        'utm' should only be used for small polygons when centimeter
        level accuraccy is needed.  Othewise the area will
        be incorrect.  Similar issues can happen when polygons cross
        utm boundaries.
        """
        if proj is None:
            return self.map(lambda x: x.GetArea())

        if proj == 'utm':
            if self.proj.ExportToProj4().strip() != ut.PROJ_WGS84:
                vl = self.transform(ut.projection_from_string())
            else:
                vl = self

            shps = vl.to_shapely()
            areas = [ops.transform(to_utm, shp).area for shp in shps]
            s = pd.Series(areas, index=self.index)
            s.name = "area_sqr_m"
            return s

        elif proj == 'albers':
            proj = ut.projection_from_string(ut.ALBERS_N_AMERICA)

        return self.transform(proj).areas()