Esempio n. 1
0
 def test_read_shapefile(self):
     test_shapefile = os.path.join(this_dir, "test_layer", "layer.shp")
     reader = shapefile_util.get_shapefile_reader(test_shapefile)
     for r in reader.records():
         print r
     print reader.get_schema()
     print reader.get_mbr()
Esempio n. 2
0
 def test_read_shapefile(self):
     test_shapefile = os.path.join(this_dir, "test_layer", "layer.shp")
     reader = shapefile_util.get_shapefile_reader(test_shapefile)
     for r in reader.records():
         print r
     print reader.get_schema()
     print reader.get_mbr()
Esempio n. 3
0
 def __init__(self,
              shp_file=None,
              force_multipolygon=True,
              reproject_to=None,
              **kwargs):
     self.shp_file = shp_file
     self.force_multipolygon = force_multipolygon
     self.reproject_to = reproject_to
     self.reader = shapefile_util.get_shapefile_reader(self.shp_file)
Esempio n. 4
0
    def create_substrates_layer(self):
        logger = self.get_logger_for_section(
            section_id='substrates_layer', 
            base_msg="Creating substrates layer...")
        # Read substrates.
        substrates = []
        substrates_path = os.path.join(
            self.source_dir, 'substrates.csv')
        with open(substrates_path, 'rb') as f:
            for row in csv.DictReader(f):
                css_color = row.get('color', '#000000')
                hex_color = css_color.lstrip('#')
                row['rgb'] = [int(hex_color[i:i+2],16) for i in range(0, 6, 2)]
                substrates.append(row)

        # Make substrate shapefile from habitats, 
        # with EPSG:3857 as the CRS.
        layer_dir = os.path.join(self.layers_dir, 'substrates')
        os.makedirs(layer_dir)
        habs_shapefile = os.path.join(
            self.source_dir, 'habitats', 'habitats.shp')
        logger.info("Reading shapes from habitats file...")
        reader = shapefile_util.get_shapefile_reader(habs_shapefile)
        source_mbr = reader.get_mbr()
        source_crs = reader.get_crs()
        source_schema = reader.get_schema()
        
        write_msg = "Writing shapes..."
        logger.info(write_msg)
        reader = shapefile_util.get_shapefile_reader(habs_shapefile)
        substrate_shapefile = os.path.join(layer_dir, 'substrates.shp')
        writer = shapefile_util.get_shapefile_writer(
            shapefile=substrate_shapefile, 
            crs='EPSG:3857',
            schema=source_schema,
        )
        skipped = 0
        counter = 0
        log_interval = 1e3
        for record in reader.records():
            counter += 1
            if (counter % log_interval) == 0:
                prog_msg = "%s %d of %d (%.1f)" % (
                    write_msg, counter, reader.size, 
                    100.0 * counter/reader.size)
                logger.info(prog_msg)
            shp = gis_util.geojson_to_shape(record['geometry'])
            proj_shp = gis_util.reproject_shape(shp, source_crs, 'EPSG:3857')
            record['geometry'] = json.loads(gis_util.shape_to_geojson(proj_shp))
            bad_rec = False
            ps = record['properties']
            if type(ps['ENERGY']) is not unicode:
                bad_rec = True
            if type(ps['Z']) is not float:
                bad_rec = True
            if type(ps['SUBSTRATE']) is not unicode:
                bad_rec = True
            if record['geometry']['type'] != source_schema['geometry']:
                bad_rec = True

            if not bad_rec:
                writer.write(record)
            else:
                skipped += 1

        writer.close()
        reader.close()
        if skipped:
            self.logger.info("%s Skipped %s records due to formatting" %
                             (write_msg, skipped))

        # Transform bounds to EPSG:3857.
        mbr_diag = gis_util.wkt_to_shape(
            "LINESTRING (%s %s, %s %s)" % source_mbr)
        projected_diag = gis_util.reproject_shape(
            mbr_diag, source_crs, 'EPSG:3857')
        projected_mbr = gis_util.get_shape_mbr(projected_diag)

        # Write layer client config.
        client_path = os.path.join(layer_dir, 'client.json')
        info_template = self.template_env.get_template(
            'georefine/substrates_info.html')
        info_html = info_template.render(substrates=substrates)
        client_config = {
            'id': 'substrates',
            'label': 'Substrates',
            'source': 'georefine_wms',
            'layer_type': "WMS",
            'params': {
                'srs':'EPSG:3857',
                'transparent': True,
                'layers': 'substrates',
            },
            'info': info_html,
            'properties': {
                'maxExtent': projected_mbr,
                'projection': 'EPSG:3857',
                'serverResolutions': [4891.96981024998, 2445.98490512499, 1222.99245256249, 611.49622628138, 305.748113140558],
                'tileSize': {'w': 512, 'h': 512},
                'visibility': False
            },
            'zIndex': 30,
        }
        with open(client_path, 'wb') as f:
            json.dump(client_config, f)

        # Write mapfile.
        mapfile_path = os.path.join(layer_dir, 'substrates.map')
        with open(mapfile_path, 'wb') as f:
            mapfile_template = self.template_env.get_template(
                'georefine/substrates.map')
            f.write(mapfile_template.render(
                substrates=substrates,
                mbr=projected_mbr
            ))

        # Write wms config.
        wms_config_path = os.path.join(layer_dir, 'wms.json')
        wms_config = {
            'mapfile': 'substrates.map'
        }
        with open(wms_config_path, 'wb') as f:
            json.dump(wms_config, f)
Esempio n. 5
0
 def __init__(self, shp_file=None, force_multipolygon=True,
              reproject_to=None, **kwargs):
     self.shp_file = shp_file
     self.force_multipolygon = force_multipolygon
     self.reproject_to = reproject_to
     self.reader = shapefile_util.get_shapefile_reader(self.shp_file)