Python shapefile.Reader() Examples

The following are 30 code examples of shapefile.Reader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module shapefile , or try the search function .
Example #1
Source File: spatial.py    From swmmio with MIT License 6 votes vote down vote up
def read_shapefile(shp_path):
    """
        Read a shapefile into a Pandas dataframe with a 'coords' column holding
        the geometry information. This uses the pyshp package
        """
    import shapefile

    # read file, parse out the records and shapes
    sf = shapefile.Reader(shp_path)
    fields = [x[0] for x in sf.fields][1:]
    records = sf.records()
    shps = [s.points for s in sf.shapes()]

    # write into a dataframe
    df = pd.DataFrame(columns=fields, data=records)
    df = df.assign(coords=shps)

    return df 
Example #2
Source File: utils.py    From PyCINRAD with GNU General Public License v3.0 6 votes vote down vote up
def highlight_area(
    area: Union[Array_T, str], linecolor: str = "red", **kwargs
) -> List[Line2D]:
    r"""Return list of Line2D object for given area name"""
    fpath = os.path.join(MODULE_DIR, "data", "shapefile", "City")
    shp = shapefile.Reader(fpath, encoding="gbk")
    rec = shp.shapeRecords()
    lines = list()
    if isinstance(area, str):
        area = [area]
    for i in area:
        if not isinstance(i, str):
            raise RadarPlotError("Area name should be str")
        name = np.array([i.record[2] for i in rec])
        target = np.array(rec)[(name == i).nonzero()[0]]
        for j in target:
            pts = j.shape.points
            x = [i[0] for i in pts]
            y = [i[1] for i in pts]
            lines.append(Line2D(x, y, color=linecolor))
    return lines 
Example #3
Source File: geographical_view.py    From pyNMS with GNU General Public License v3.0 6 votes vote down vote up
def draw_polygons(self):
        sf = shapefile.Reader(self.shapefile)       
        polygons = sf.shapes() 
        for polygon in polygons:
            # convert shapefile geometries into shapely geometries
            # to extract the polygons of a multipolygon
            polygon = shapely.geometry.shape(polygon)
            # if it is a polygon, we use a list to make it iterable
            if polygon.geom_type == 'Polygon':
                polygon = [polygon]
            for land in polygon:
                qt_polygon = QtGui.QPolygonF() 
                longitudes, latitudes = land.exterior.coords.xy
                for lon, lat in zip(longitudes, latitudes):
                    px, py = self.to_canvas_coordinates(lon, lat)
                    if px > 1e+10:
                        continue
                    qt_polygon.append(QtCore.QPointF(px, py))
                polygon_item = QtWidgets.QGraphicsPolygonItem(qt_polygon)
                polygon_item.setBrush(self.land_brush)
                polygon_item.setPen(self.land_pen)
                polygon_item.setZValue(1)
                yield polygon_item 
Example #4
Source File: layers.py    From geoplotlib with MIT License 6 votes vote down vote up
def __init__(self, fname, f_tooltip=None, color=None, linewidth=3, shape_type='full', encoding='utf-8', encodingErrors='strict'):
        """
        Loads and draws shapefiles

        :param fname: full path to the shapefile
        :param f_tooltip: function to generate a tooltip on mouseover
        :param color: color
        :param linewidth: line width
        :param shape_type: either full or bbox
        """
        if color is None:
            color = [255, 0, 0]
        self.color = color
        self.linewidth = linewidth
        self.f_tooltip = f_tooltip
        self.shape_type = shape_type

        try:
            import shapefile
        except:
            raise Exception('ShapefileLayer requires pyshp')

        self.reader = shapefile.Reader(fname, encoding=encoding, encodingErrors=encodingErrors)
        self.worker = None
        self.queue = Queue.Queue() 
Example #5
Source File: pyGISS.py    From pyGISS with MIT License 6 votes vote down vote up
def draw_polygons(self):
        sf = shapefile.Reader(self.shapefile)       
        polygons = sf.shapes() 
        for polygon in polygons:
            polygon = shapely.geometry.shape(polygon)
            if polygon.geom_type == 'Polygon':
                polygon = [polygon]
            for land in polygon:
                qt_polygon = QPolygonF() 
                for lon, lat in land.exterior.coords:
                    px, py = self.to_canvas_coordinates(lon, lat)
                    if px > 1e+10:
                        continue
                    qt_polygon.append(QPointF(px, py))
                polygon_item = QGraphicsPolygonItem(qt_polygon)
                polygon_item.setBrush(QBrush(QColor(52, 165, 111)))
                polygon_item.setZValue(1)
                yield polygon_item 
Example #6
Source File: extended_pyGISS.py    From pyGISS with MIT License 6 votes vote down vote up
def draw_polygons(self):
        sf = shapefile.Reader(self.shapefile)       
        polygons = sf.shapes() 
        for polygon in polygons:
            # convert shapefile geometries into shapely geometries
            # to extract the polygons of a multipolygon
            polygon = shapely.geometry.shape(polygon)
            # if it is a polygon, we use a list to make it iterable
            if polygon.geom_type == 'Polygon':
                polygon = [polygon]
            for land in polygon:
                qt_polygon = QPolygonF() 
                for lon, lat in land.exterior.coords:
                    print(lon, lat)
                    px, py = self.to_canvas_coordinates(lon, lat)
                    if px > 1e+10:
                        continue
                    qt_polygon.append(QPointF(px, py))
                polygon_item = QGraphicsPolygonItem(qt_polygon)
                polygon_item.setBrush(self.land_brush)
                polygon_item.setPen(self.land_pen)
                polygon_item.setZValue(1)
                yield polygon_item 
Example #7
Source File: pyGISS.py    From pyGISS with MIT License 6 votes vote down vote up
def draw_map(self):
        self.delete('land', 'water')
        self.draw_water()
        sf = shapefile.Reader(self.filepath)
        polygons = sf.shapes()
        for polygon in polygons:
            # convert shapefile geometries into shapely geometries
            # to extract the polygons of a multipolygon
            polygon = shapely.geometry.shape(polygon)
            if polygon.geom_type == 'Polygon':
                polygon = [polygon]
            for land in polygon:
                self.create_polygon(
                    sum((self.to_canvas_coordinates(*c) for c in land.exterior.coords), ()),
                    fill='green3',
                    outline='black',
                    tags=('land',)
                ) 
Example #8
Source File: extended_pyGISS.py    From pyGISS with MIT License 6 votes vote down vote up
def draw_map(self):
        if not self.filepath:
            return
        self.delete('land', 'water')
        self.ratio, self.offset = 1, (0, 0)
        self.draw_water()
        sf = shapefile.Reader(self.filepath)       
        polygons = sf.shapes() 
        for polygon in polygons:
            polygon = shapely.geometry.shape(polygon)
            if polygon.geom_type == 'Polygon':
                polygon = [polygon]
            for land in polygon:
                self.create_polygon(
                    sum((self.to_canvas_coordinates(*c) for c in land.exterior.coords), ()),    
                    fill = 'green3', 
                    outline = 'black', 
                    tags = ('land',)
                )
        self.redraw_nodes() 
Example #9
Source File: Boundaries.py    From wa with Apache License 2.0 6 votes vote down vote up
def Determine(Basin, Home_folder):
      			
    Shape_file_name_shp = os.path.join(Home_folder,'Basins', Basin + '.shp')
    if not os.path.exists(Shape_file_name_shp):
        print '%s is missing' %Shape_file_name_shp				
    Shape_file_name_dbf = os.path.join(Home_folder,'Basins', Basin + '.dbf')
    if not os.path.exists(Shape_file_name_dbf):
        print '%s is missing' %Shape_file_name_dbf					

    Basin_shp = shapefile.Reader(Shape_file_name_shp, Shape_file_name_dbf)
    Shape = Basin_shp.shapes()
    bbox = Shape[0].bbox
    Boundaries = dict()
    #Boundaries['Lonmin'] = np.floor(bbox[0]) - 0.1
    #Boundaries['Lonmax'] = np.ceil(bbox[2]) + 0.1
    #Boundaries['Latmin'] = np.floor(bbox[1]) - 0.1
    #Boundaries['Latmax'] = np.ceil(bbox[3]) + 0.1
    Boundaries['Lonmin'] = round(np.floor((bbox[0] * 10.) - 1.))/10.
    Boundaries['Lonmax'] = round(np.ceil((bbox[2] * 10.) + 1.))/10.
    Boundaries['Latmin'] = round(np.floor((bbox[1] * 10.) - 1.))/10.
    Boundaries['Latmax'] = round((np.ceil(bbox[3] * 10.) + 1.))/10.				
    return(Boundaries, Shape_file_name_shp) 
Example #10
Source File: building_properties.py    From CityEnergyAnalyst with MIT License 6 votes vote down vote up
def calc_bounding_box_geom(self, geometry_shapefile):
        import shapefile
        sf = shapefile.Reader(geometry_shapefile)
        shapes = sf.shapes()
        len_shapes = len(shapes)
        bwidth = []
        blength = []
        for shape in range(len_shapes):
            bbox = shapes[shape].bbox
            coords_bbox = [coord for coord in bbox]
            delta1 = abs(coords_bbox[0] - coords_bbox[2])
            delta2 = abs(coords_bbox[1] - coords_bbox[3])
            if delta1 >= delta2:
                bwidth.append(delta2)
                blength.append(delta1)
            else:
                bwidth.append(delta1)
                blength.append(delta2)

        return blength, bwidth 
Example #11
Source File: lodes.py    From census_area with MIT License 6 votes vote down vote up
def _area(self, report_id, year):

        url = self.BASE + '/cgi-bin/report.py'

        analysis = {"analysis_type": "area_profile_report",
                    "job_type": 'jt00',
                    "ap_segment": "s000",
                    "origin": 'home',
                    "color": ["#0000AA"]}

        params = {'report_id': report_id,
                  'settings': json.dumps(analysis),
                  'mode': 'export_geography',
                  'format': 'shp'}

        response = self.get(url, params=params)

        z = zipfile.ZipFile(io.BytesIO(response.content))

        with z.open('points_{year}.dbf'.format(year=year)) as dbf:
            reader = shapefile.Reader(dbf=io.BytesIO(dbf.read()))
            fields = [field[0] for field in reader.fields[1:]]
            for row in reader.records():
                yield dict(zip(fields, row)) 
Example #12
Source File: shapefile_to_plt.py    From handyscripts with MIT License 6 votes vote down vote up
def main(shapefilename, outfilename):
    # define index from record for zone name
    s = sf.Reader(shapefilename)
    shape_records = s.shapeRecords()

    conversion_option = get_conversion_option(shape_records)

    if get_var_names() == 0:
        x_var_name = 'x'
        y_var_name = 'y'
    else:
        x_var_name = 'lon'
        y_var_name = 'lat'
    dataset = tp.active_frame().create_dataset("Shapefile", [x_var_name, y_var_name])

    if conversion_option == 1:  # Single Zone
        start = time.time()
        convert_to_single_zone(s, os.path.basename(shapefilename), dataset)
    else:  # One Zone per Shape
        name_index = get_name_index(s)
        start = time.time()
        convert_to_one_zone_per_shape(s, name_index, dataset)
    tp.data.save_tecplot_plt(outfilename)
    print("Elapsed time: ", time.time() - start) 
Example #13
Source File: utils.py    From PyCINRAD with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, filename: str, encoding: str = "gbk"):
        # Validate the filename/shapefile
        self._reader = reader = shapefile.Reader(filename, encoding=encoding)
        if reader.shp is None or reader.shx is None or reader.dbf is None:
            raise ValueError("Incomplete shapefile definition " "in '%s'." % filename)
        try:
            shapeType = reader.shapeType
            self._geometry_factory = shapereader.GEOMETRY_FACTORIES.get(shapeType)
            if self._geometry_factory is None:
                raise ValueError("Unsupported shape type: %s" % shapeType)
        except AttributeError:
            pass
        self._fields = self._reader.fields 
Example #14
Source File: pyEarth.py    From pyEarth with MIT License 5 votes vote down vote up
def extract_polygons(self):
        if not hasattr(self, 'shapefile'):
            return
        sf = shapefile.Reader(self.shapefile)       
        polygons = sf.shapes() 
        for polygon in polygons:
            polygon = shapely.geometry.shape(polygon)
            yield from [polygon] if polygon.geom_type == 'Polygon' else polygon 
Example #15
Source File: cw_base.py    From pycoast with GNU General Public License v3.0 5 votes vote down vote up
def _add_shapefile_shapes(self, image, area_def, filename,
                              feature_type=None, **kwargs):
        """Draw all shapes (polygon/poly-lines) from a shape file onto a PIL Image."""
        sf = shapefile.Reader(filename)
        return self.add_shapes(image, area_def, sf.shapes(), feature_type=feature_type, **kwargs) 
Example #16
Source File: cw_base.py    From pycoast with GNU General Public License v3.0 5 votes vote down vote up
def _add_shapefile_shape(self, image, area_def, filename, shape_id,
                             feature_type=None, **kwargs):
        """ for drawing a single shape (polygon/poly-line) definiton with id,
        shape_id from a custom shape file onto a PIL image
        """
        sf = shapefile.Reader(filename)
        shape = sf.shape(shape_id)
        return self.add_shapes(image, area_def, [shape], feature_type=feature_type, **kwargs) 
Example #17
Source File: lodes.py    From census_area with MIT License 5 votes vote down vote up
def _od(self, report_id, year):

        url = self.BASE + '/cgi-bin/report.py'

        analysis = {"labor": "s000",
                    "year": 2014,
                    "analysis_type": "distance_direction_report",
                    "job_type": 'jt01',
                    "origin_type": 'work',
                    "distance":"all",
                    "color":["#0000AA"]}

        params = {'report_id': report_id,
                  'settings': json.dumps(analysis),
                  'mode': 'export_geography',
                  'format': 'shp'}

        response = self.get(url, params=params)

        z = zipfile.ZipFile(io.BytesIO(response.content))

        with z.open('points_{year}.dbf'.format(year=year)) as dbf:
            reader = shapefile.Reader(dbf=io.BytesIO(dbf.read()))
            fields = [field[0] for field in reader.fields[1:]]
            for row in reader.records():
                yield dict(zip(fields, row)) 
Example #18
Source File: __init__.py    From georef-ar-api with MIT License 5 votes vote down vote up
def shapefile_from_zip_bytes(data):
    """Dada una secuencia de bytes representando un zipped Shapefile,
    devuelve una instancia de shapefile.Reader con sus contenidos.

    Args:
        data (bytes): Secuencia de bytes (ZIP).

    Return:
        shapefile.Reader: Contenidos del Shapefile.

    """
    contents = io.BytesIO(data)
    zip_file = zipfile.ZipFile(contents)

    files = {}
    for filename in zip_file.namelist():
        extension = os.path.splitext(filename)[1][1:]
        buf = io.BytesIO()

        with zip_file.open(filename) as f:
            shutil.copyfileobj(f, buf)

        buf.seek(0)
        files[extension] = buf

    return shapefile.Reader(**files) 
Example #19
Source File: read_shapefile.py    From landlab with MIT License 5 votes vote down vote up
def _read_shapefile(file, dbf):
    if isinstance(file, pathlib.PurePath):
        file = str(file)
    try:
        sf = ps.Reader(file)
    except ShapefileException:
        try:
            sf = ps.Reader(shp=file, dbf=dbf)
        except ShapefileException:
            raise ShapefileException(("Bad file path provided to read_shapefile."))
    return sf 
Example #20
Source File: mapper.py    From aws_taxi with MIT License 5 votes vote down vote up
def readNeighborhood(shapeFilename, index, neighborhoods):
    sf = shapefile.Reader(shapeFilename)
    for sr in sf.shapeRecords():
        if sr.record[1] not in ['New York', 'Kings', 'Queens', 'Bronx']: continue
        paths = map(Path, numpy.split(sr.shape.points, sr.shape.parts[1:]))
        bbox = paths[0].get_extents()
        map(bbox.update_from_path, paths[1:])
        index.insert(len(neighborhoods), list(bbox.get_points()[0])+list(bbox.get_points()[1]))
        neighborhoods.append((sr.record[3], paths))
    neighborhoods.append(('UNKNOWN', None)) 
Example #21
Source File: model.py    From photonix with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_world(self, world_file):
        return shapefile.Reader(world_file, encoding='latin1').shapeRecords() 
Example #22
Source File: body.py    From termtrack with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, width, height):
        self.height = height
        self.width = width
        self.pixel_percentage = 100 / (self.width * self.height)
        self._img = Image.open(join(dirname(__file__), "data", self.COLORMAP))
        if self.SHAPEFILE is not None:
            self._sf = shapefile.Reader(join(dirname(__file__), "data", self.SHAPEFILE)) 
Example #23
Source File: Map.py    From PyMICAPS with GNU General Public License v2.0 5 votes vote down vote up
def DrawShapeFile(area):
        """
        在画布上绘制shp文件
        :param area: 包含shp文件名及线宽和线颜色的一个字典
        :return: 
        """
        try:
            shpfile = area.file
            border_shape = shapefile.Reader(shpfile)
            border = border_shape.shapes()
            for b in border:
                border_points = b.points
                path_data = []
                count = 0
                for cell in border_points:
                    if count == 0:
                        trans = (Path.MOVETO, (cell[0], cell[1]))
                        path_data += [trans]
                        cell_end = cell
                    else:
                        trans = (Path.CURVE4, (cell[0], cell[1]))
                        path_data += [trans]
                trans = (Path.CLOSEPOLY, (cell_end[0], cell_end[1]))
                path_data += [trans]

                codes, verts = list(zip(*path_data))
                path = Path(verts, codes)
                x, y = list(zip(*path.vertices))
                plt.plot(x, y, 'k-', linewidth=area.linewidth, color=area.linecolor)
        except Exception as err:
            print(u'【{0}】{1}-{2}'.format(area['file'], err, datetime.now())) 
Example #24
Source File: extended_pyEarth.py    From pyEarth with MIT License 5 votes vote down vote up
def draw_polygons(self):
        if not hasattr(self, 'shapefile'):
            return
        sf = shapefile.Reader(self.shapefile)       
        polygons = sf.shapes() 
        for polygon in polygons:
            polygon = shapely.geometry.shape(polygon)
            yield from [polygon] if polygon.geom_type == 'Polygon' else polygon 
Example #25
Source File: census_tracts_kml.py    From Spectrum-Access-System with Apache License 2.0 5 votes vote down vote up
def ProcessShpFile(f, basename, kml):
  print 'Processing shp file %s' % basename
  with zipfile.ZipFile(f) as zf:
    shpfile = io.BytesIO(zf.read(basename + '.shp'))
    dbffile = io.BytesIO(zf.read(basename + '.dbf'))
    shxfile = io.BytesIO(zf.read(basename + '.shx'))

  shpfile = shapefile.Reader(shp=shpfile, shx=shxfile, dbf=dbffile)
  geoidField = -1
  alandField = -1
  awaterField = -1
  for i in range(0, len(shpfile.fields)):
    field = shpfile.fields[i][0]
    if 'GEOID' in field:
      geoidField = i - 1
    elif 'ALAND' in field:
      alandField = i - 1
    elif 'AWATER' in field:
      awaterField = i - 1
  if geoidField == -1 or alandField == -1 or awaterField == -1:
    raise Exception('Could not find GEOID,ALAND,AWATER in fields %s' % shpfile.fields)

  shapes = 0
  print 'Converting %d shapes' % shpfile.numRecords
  for i in range(0, shpfile.numRecords):
    geoid = shpfile.record(i)[geoidField]
    aland = shpfile.record(i)[alandField]
    awater = shpfile.record(i)[awaterField]
    placemark = ConvertShapeToPlacemark(shpfile.shape(i), geoid, aland, awater, kml)
    shapes += 1
    kml.Document.append(placemark)
  print 'Converted %d rings' % shapes
  zf.close()


# Process a .zip file containing shapefile files and
# create a KML file containing all the polygons described. 
Example #26
Source File: building_process_hvac_efficiencies.py    From CityEnergyAnalyst with MIT License 5 votes vote down vote up
def get_building_length_and_width(locator,
                                  buildings_names
                                  ):
    # Function taken from calc_bounding_box_geom in the CEA file building_properties.py
    # Get data
    geometry_shapefile_path = locator.get_zone_geometry()

    # Calculate
    import shapefile
    sf = shapefile.Reader(geometry_shapefile_path)
    shapes = sf.shapes()
    len_shapes = len(shapes)
    length_and_width = []
    for shape in range(len_shapes):
        bbox = shapes[shape].bbox
        coords_bbox = [coord for coord in bbox]
        delta1 = abs(coords_bbox[0] - coords_bbox[2])
        delta2 = abs(coords_bbox[1] - coords_bbox[3])
        if delta1 >= delta2:
            Lw = delta2
            Ll = delta1
            length_and_width.append([Ll, Lw])
        else:
            Lw = delta1
            Ll = delta2
            length_and_width.append([Ll, Lw])

    for i in range(len(buildings_names)):
        length_and_width[i].insert(0, buildings_names[i])

    length_and_width_df = pd.DataFrame(
        length_and_width,
        columns=[
            'Name',
            'Ll',
            'Lw'
        ]
    )
    length_and_width_df.set_index('Name', inplace=True)

    return length_and_width_df 
Example #27
Source File: bbd2kite.py    From kite with GNU General Public License v3.0 5 votes vote down vote up
def read_shapefile(filename):
    log.info('Loading data from %s', filename)
    shp = shapefile.Reader(filename)

    npoints = shp.numRecords
    field_name_map = {fld[0].lower(): fld[0] for fld in shp.fields}

    data = DataStruct()
    data.bbox = shp.bbox

    data.ps_mean_v = num.zeros(npoints)
    data.ps_mean_var = num.zeros(npoints)
    los_n = num.zeros(npoints)
    los_e = num.zeros(npoints)
    los_u = num.zeros(npoints)

    coords = num.zeros((npoints, 2))

    for isr, sr in enumerate(shp.iterShapeRecords()):
        shape = sr.shape
        record = sr.record
        # assert shape.shapeType == 11

        los_n[isr] = getattr(record, field_name_map['los_north'])
        los_e[isr] = getattr(record, field_name_map['los_east'])
        los_u[isr] = -getattr(record, field_name_map['los_up'])

        data.ps_mean_v[isr] = getattr(record, field_name_map['mean_velo'])
        data.ps_mean_var[isr] = getattr(record, field_name_map['var_mean_v'])

        coords[isr] = shape.points[0]

    data.phi = num.arctan2(los_n, los_e)
    data.theta = num.arcsin(los_u)

    data.easts = coords[:, 0]
    data.norths = coords[:, 1]

    return data 
Example #28
Source File: test_deploy.py    From pyvisgraph with MIT License 5 votes vote down vote up
def setup_method(self, method):
        self.sf = shapefile.Reader("examples/shapefiles/GSHHS_c_L1.dbf")
        self.shapes = self.sf.shapes()
        self.polys = []
        self.polys.append([vg.Point(a[0], a[1]) for a in self.shapes[4].points]) 
Example #29
Source File: usa.py    From xy with MIT License 5 votes vote down vote up
def load_shapes(path):
    skip = set(['02', '15', '60', '66', '69', '78', '72'])
    result = []
    sf = shapefile.Reader(path)
    for item in sf.shapeRecords():
        if item.record[0] in skip:
            continue
        result.extend(shape_to_polygons(item.shape))
    return result 
Example #30
Source File: maskout.py    From PyMICAPS with GNU General Public License v2.0 5 votes vote down vote up
def getPathFromShp(shpfile, region):
    try:
        sf = shapefile.Reader(shpfile)
        vertices = []  # 这块是已经修改的地方
        codes = []  # 这块是已经修改的地方
        paths = []
        for shape_rec in sf.shapeRecords():
            # if shape_rec.record[3] == region:  # 这里需要找到和region匹配的唯一标识符,record[]中必有一项是对应的。
            if region == [100000] or shape_rec.record[4] in region:  # 这块是已经修改的地方
                pts = shape_rec.shape.points
                prt = list(shape_rec.shape.parts) + [len(pts)]
                for i in range(len(prt) - 1):
                    for j in range(prt[i], prt[i + 1]):
                        vertices.append((pts[j][0], pts[j][1]))
                    codes += [Path.MOVETO]
                    codes += [Path.LINETO] * (prt[i + 1] - prt[i] - 2)
                    codes += [Path.CLOSEPOLY]
                path = Path(vertices, codes)
                paths.append(path)
        if paths:
            path = Path.make_compound_path(*paths)
        else:
            path = None
        return path
    except Exception as err:
        print(err)
        return None