コード例 #1
0
    def test_from_dict(self):
        data = {"A": [1], "geometry": [Point(0.0, 0.0)]}
        df = GeoDataFrame.from_dict(data, crs=3857)
        assert df.crs == "epsg:3857"
        assert df._geometry_column_name == "geometry"

        data = {"B": [1], "location": [Point(0.0, 0.0)]}
        df = GeoDataFrame.from_dict(data, geometry="location")
        assert df._geometry_column_name == "location"
コード例 #2
0
 def to_geodataframe(self, columns=["geometry"]):
     data = {}
     for key, value in self.items():
         data[key] = {}
         for column in columns:
             data[key][column] = getattr(value, column)
     return GeoDataFrame.from_dict(data=data, orient="index", crs=self.crs)
コード例 #3
0
    def add_qgeometry(
            self,
            kind: str,
            component_name: str,
            geometry: dict,
            subtract: bool = False,
            helper: bool = False,
            layer: Union[int, str] = 1,  # chip will be here
            chip: str = 'main',
            **other_options):
        """Main interface to add qgeometries.

        Args:
            kind (str): Must be in get_element_types ('path', 'poly', etc.).
            component_name (str): Component name.
            geometry (dict): Dict of shapely geometry.
            subtract (bool): Subtract - passed through.  Defaults to False.
            helper (bool): Helper - passed through.  Defaults to False.
            layer (Union[int, str]): Layer - passed through.  Defaults to 1.
            chip (str): Chip name - passed through.  Defaults to 'main'.
            **other_options (object): Other_options - passed through.
        """
        # TODO: Add unit test

        # ensure correct types
        if not isinstance(subtract, bool):
            subtract = subtract in TRUE_BOOLS
        if not isinstance(helper, bool):
            helper = helper in TRUE_BOOLS

        if not (kind in self.get_element_types()):
            self.logger.error(
                f'Creator user error: Unknown element kind=`{kind}`'
                f'Kind must be in {self.get_element_types()}. This failed for component'
                f'name = `{component_name}`.\n'
                f' The call was with subtract={subtract} and helper={helper}'
                f' and layer={layer}, and options={other_options}')

        #Checks if (any) of the geometry are MultiPolygons, and breaks them up into
        #individual polygons. Rounds the coordinate sequences of those values to avoid
        #numerical errors.
        rounding_val = self.design.template_options['PRECISION']
        new_dict = Dict()
        for key, item in geometry.items():
            if isinstance(geometry[key], MultiPolygon):
                temp_multi = geometry[key]
                shape_count = 0
                for shape_temp in temp_multi.geoms:
                    new_dict[key + '_' +
                             str(shape_count)] = round_coordinate_sequence(
                                 shape_temp, rounding_val)
                    shape_count += 1
            else:
                new_dict[key] = round_coordinate_sequence(item, rounding_val)

        geometry = new_dict

        # Create options TODO: Might want to modify this (component_name -> component_id)
        # Give warning if length is to be fillet's and not long enough.
        self.check_lengths(geometry, kind, component_name, **other_options)

        # Create options
        options = dict(component=component_name,
                       subtract=subtract,
                       helper=helper,
                       layer=int(layer),
                       chip=chip,
                       **other_options)

        #replaces line above to generate the options.
        #for keyC in design.qgeometry.tables[kind].columns:
        #    if keyC != 'geometry':
        #        options[keyC] = ???[keyC] -> alternative manner to pass options to the add_qgeometry function?
        #                                       instead have the add_qeometry in baseComponent generate the dict?

        #Could we just append rather than make a new table each time? This seems slow
        table = self.tables[kind]

        # assert that all names in options are in table columns! TODO: New approach will not be wanting
        #to do this (maybe check that all columns are in options?)
        df = GeoDataFrame.from_dict(geometry,
                                    orient='index',
                                    columns=['geometry'])
        df.index.name = 'name'
        df = df.reset_index()

        df = df.assign(**options)

        # Set new table. Unfortunately, this creates a new instance. Can just direct append
        self.tables[kind] = table.append(df, sort=False, ignore_index=True)