Exemplo n.º 1
0
    def collapse_zip_codes(self):
        # The ESRI ZIP Code layer breaks ZIP Codes up along county
        # boundaries, so we need to collapse them first before
        # proceeding

        if len(self.zipcodes) > 0:
            return

        for feature in self.layer:
            zipcode = feature.get(self.name_field)
            geom = feature.geom.geos
            if zipcode not in self.zipcodes:
                self.zipcodes[zipcode] = geom
            else:
                # If it's a MultiPolygon geom we're adding to our
                # existing geom, we need to "unroll" it into its
                # constituent polygons 
                if isinstance(geom, MultiPolygon):
                    subgeoms = list(geom)
                else:
                    subgeoms = [geom]
                existing_geom = self.zipcodes[zipcode]
                if not isinstance(existing_geom, MultiPolygon):
                    new_geom = MultiPolygon([existing_geom])
                    new_geom.extend(subgeoms)
                    self.zipcodes[zipcode] = new_geom
                else:
                    existing_geom.extend(subgeoms)
Exemplo n.º 2
0
    def save(self, verbose=False):
        # The ESRI ZIP Code layer breaks ZIP Codes up along county
        # boundaries, so we need to collapse them first before
        # proceeding
        zipcodes = {}
        for feature in self.layer:
            zipcode = feature.get('POSTAL')
            geom = feature.geom.geos
            if zipcode not in zipcodes:
                zipcodes[zipcode] = geom
            else:
                # If it's a MultiPolygon geom we're adding to our
                # existing geom, we need to "unroll" it into its
                # constituent polygons
                if isinstance(geom, MultiPolygon):
                    subgeoms = list(geom)
                else:
                    subgeoms = [geom]
                existing_geom = zipcodes[zipcode]
                if not isinstance(existing_geom, MultiPolygon):
                    new_geom = MultiPolygon([existing_geom])
                    new_geom.extend(subgeoms)
                    zipcodes[zipcode] = new_geom
                else:
                    existing_geom.extend(subgeoms)

        sorted_zipcodes = sorted(zipcodes.iteritems(), key=lambda x: int(x[0]))
        now = datetime.datetime.now()
        num_created = 0
        for i, (zipcode, geom) in enumerate(sorted_zipcodes):
            if not geom.valid:
                geom = geom.buffer(0.0)
                if not geom.valid:
                    print >> sys.stderr, 'Warning: invalid geometry for %s' % zipcode
            geom.srid = 4326
            zipcode_obj, created = Location.objects.get_or_create(
                name=zipcode,
                normalized_name=zipcode,
                slug=zipcode,
                location_type=self.location_type,
                location=geom,
                centroid=geom.centroid,
                display_order=i,
                city=self.city,
                source='ESRI',
                area=geom.transform(3395, True).area,
                is_public=True,
                creation_date=now,
                last_mod_date=now,
            )
            if created:
                num_created += 1
            if verbose:
                print >> sys.stderr, '%s ZIP Code %s ' % (
                    created and 'Created' or 'Already had', zipcode_obj.name)
        return num_created
Exemplo n.º 3
0
    def save(self, verbose=False):
        # The ESRI ZIP Code layer breaks ZIP Codes up along county
        # boundaries, so we need to collapse them first before
        # proceeding
        zipcodes = {}
        for feature in self.layer:
            zipcode = feature.get('POSTAL')
            geom = feature.geom.geos
            if zipcode not in zipcodes:
                zipcodes[zipcode] = geom
            else:
                # If it's a MultiPolygon geom we're adding to our
                # existing geom, we need to "unroll" it into its
                # constituent polygons 
                if isinstance(geom, MultiPolygon):
                    subgeoms = list(geom)
                else:
                    subgeoms = [geom]
                existing_geom = zipcodes[zipcode]
                if not isinstance(existing_geom, MultiPolygon):
                    new_geom = MultiPolygon([existing_geom])
                    new_geom.extend(subgeoms)
                    zipcodes[zipcode] = new_geom
                else:
                    existing_geom.extend(subgeoms)

        sorted_zipcodes = sorted(zipcodes.iteritems(), key=lambda x: int(x[0]))
        now = datetime.datetime.now()
        num_created = 0
        for i, (zipcode, geom) in enumerate(sorted_zipcodes):
            if not geom.valid:
                geom = geom.buffer(0.0)
                if not geom.valid:
                    print >> sys.stderr, 'Warning: invalid geometry for %s' % zipcode
            geom.srid = 4326
            zipcode_obj, created = Location.objects.get_or_create(
                name = zipcode,
                normalized_name = zipcode,
                slug = zipcode,
                location_type = self.location_type,
                location = geom,
                centroid = geom.centroid,
                display_order = i,
                city = self.city,
                source = 'ESRI',
                area = geom.transform(3395, True).area,
                is_public = True,
                creation_date = now,
                last_mod_date = now,
            )
            if created:
                num_created += 1
            if verbose:
                print >> sys.stderr, '%s ZIP Code %s ' % (created and 'Created' or 'Already had', zipcode_obj.name)
        return num_created
Exemplo n.º 4
0
    def convert_region_to_polygon(
            region_queue_shape: RegionAdiacentCells) -> Polygon:
        try:
            if not region_queue_shape:
                logger.info(
                    'convert_region_to_polygon region_queue_shape None. Exit')
                return None

            list_shapesrow_region = region_queue_shape.get_rowshapesvertex()

            if not list_shapesrow_region:
                return None

            list_rectangles = list()

            for single_shape in list_shapesrow_region.get_listgroupvertex():
                if not single_shape:
                    continue

                list_points = list()
                for vertex in single_shape.get_listvertex():
                    if not vertex:
                        continue

                    tuple_point = vertex.to_tuple()

                    if not tuple_point:
                        continue

                    list_points.append(tuple_point)
                sub_rectangle = Polygon(list_points)
                list_rectangles.append(sub_rectangle)

            multi_polygon = MultiPolygon()
            multi_polygon.extend(list_rectangles)
            merged_polygon = multi_polygon.unary_union

            del multi_polygon

            logger.debug(
                'convert_shapes_to_list_rectangles Created, typeof: {}'.format(
                    type(merged_polygon).__name__))

            # merged_polygon = cascaded_union(list_rectangles)
            # FIXME: Check if the operations above gives the same results

            return merged_polygon
        except Exception as ex:
            logger.error(
                'convert_shapes_to_list_rectangles Exception {}'.format(ex))
            return None
def _combine(geometries):
    """
    Try to combine geometries with least computing power necessary.

    If there are both lines and polygons, all lines are first buffered
    one by one into polygons, and then added to a MultiPolygon together
    with the other polygons.

    If there are only polygons, they are combined in a single
    multipolygon.

    If there are only lines, they are combined in a single multilinestring
    """
    if len(geometries) == 1:
        return geometries[0]

    lines = [g for g in geometries
             if isinstance(g, (LineString))]
    multilines = [g for g in geometries
             if isinstance(g, (MultiLineString))]
    polygons = [g for g in geometries
                if isinstance(g, (Polygon))]
    multipolygons = [g for g in geometries
                if isinstance(g, (MultiPolygon))]

    if polygons or multipolygons:
        if lines or multilines:
            # All kinds of stuff present
            lines.extend([l for ml in multilines for l in ml])
            print 'buffering lines'
            for l in lines:
                polygons.append(l.buffer(0.0001, 2))
        result = MultiPolygon(polygons)
        for mp in multipolygons:
            result.extend(mp)
    else:
        # Only lines or multilines
        result = MultiLineString(lines)
        for ml in multilines:
            result.extend(ml)

    return result