Ejemplo n.º 1
0
 def revmatches():
     for row,shp in izip(subdict["refdata"].reader.iterRecords(), subdict["refdata"].reader.iterShapes()):
         #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
         #print "...",row[1] #row[4],row[14]
         print str(row)[:100]
         prepped = prep(asShape(shp)) # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)
         bbox = [shp.points[0][0],shp.points[0][1],shp.points[0][0],shp.points[0][1]] if shp.shapeType == pyshp.POINT else shp.bbox
         ilist = self.spindex.intersection(bbox)
         for i in ilist:
             othershp = self.reader.shape(i)
             if prepped.intersects(asShape(othershp)):
                 yield row,i
Ejemplo n.º 2
0
    def almanacs_kml(self):
        json = request.params.get('extent')
        # We need to make sure we only select almanacs with pages here,
        query = meta.Session.query(Almanac).join(Almanac.pages).distinct()
        # ... and eager-load the pages since the ktml template uses them.
        query = query.options(eagerload(Almanac.pages))

        # Tried also with contains_eager, not sure what the difference is
        # but I only get a fraction of the expected almanacs:
        #query = meta.Session.query(Almanac).join(Almanac.pages).distinct()
        #query = query.options(contains_eager(Almanac.pages))

        # Also tried using a single, second query for the pages.
        # ... NOPE, requires sqlalchemy > 0.6.0 which blows up on us,
        # maybe not compatible w/ pylons 0.9.7?
        #query = meta.Session.query(Almanac).join(Almanac.pages).distinct()
        #query = query.options(subqueryload(Almanac.pages))

        # Tried also without the explicit join().distinct(), this gives
        # back all almanac whether they have any pages or not:
        #query = meta.Session.query(Almanac).options(eagerload(Almanac.pages))

        if json is not None:
            shape = simplejson.loads(json)
            # Stupid asShape returns an Adapter instead of a Geometry.  We round
            # trip it through wkb to get the correct type.
            bbox = wkb.loads(asShape(shape).to_wkb())
            query = query.filter(func.st_intersects(Almanac.location, func.st_transform('SRID=%s;%s' % ('4326', b2a_hex(bbox.to_wkb())), storage_SRID)))

        c.almanacs = query.order_by(Almanac.modified.desc()).limit(200).all()
        response.content_type = 'application/vnd.google-earth.kml+xml kml'
        return render('/almanac/kml.mako')
Ejemplo n.º 3
0
    def _do_new_form_map(self, almanac_slug, page_slug=None):
        c.almanac = h.get_almanac_by_slug(almanac_slug)
        page = self._retrieve_page(c.almanac, page_slug)
        json = request.POST.get('feature')
        if json is None:
            abort(400)
        shape = simplejson.loads(json)
        # Stupid asShape returns a PointAdapter instead of a Point.  We round
        # trip it through wkb to get the correct type.
        location = wkb.loads(asShape(shape).to_wkb())
        location.srid = 4326

        c.map = map = Map()
        map.location = location
        map.page_id = page.id
        map.order = len(page.media)
        meta.Session.add(map)
        meta.Session.commit()

        geometry = c.map.location_4326.__geo_interface__

        c.editable = True
        return dict(html=render('/media/map/item.mako'),
                    map_id='pagemedia_%d' % map.id,
                    geometry=geometry,
                    )
Ejemplo n.º 4
0
 def _deserialize(self, data):
     json = data
     shape = simplejson.loads(json)
     location = wkb.loads(asShape(shape).to_wkb())
     # uncommenting this causes a transform error
     #location.srid = 4326
     return location
Ejemplo n.º 5
0
 def contains_point(self, point):
     with fiona.open(self.shapefile_filename) as fiona_collection:
         shapefile_record = fiona_collection.next()
         if 'geometry' in shapefile_record:
             shape = geo.asShape(shapefile_record['geometry'])
             if shape.contains(point):
                 return True
     return False
Ejemplo n.º 6
0
 def revmatches():
     for row, shp in izip(
             subdict["refdata"].reader.iterRecords(),
             subdict["refdata"].reader.iterShapes()):
         #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
         #print "...",row[1] #row[4],row[14]
         print str(row)[:100]
         prepped = prep(
             asShape(shp)
         )  # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)
         bbox = [
             shp.points[0][0], shp.points[0][1],
             shp.points[0][0], shp.points[0][1]
         ] if shp.shapeType == pyshp.POINT else shp.bbox
         ilist = self.spindex.intersection(bbox)
         for i in ilist:
             othershp = self.reader.shape(i)
             if prepped.intersects(asShape(othershp)):
                 yield row, i
 def wkt(self):
     try:
         from shapely.geometry.geo import asShape
     except ImportError:
         from pygeoif.geometry import as_shape as asShape
     try:
         return asShape(IGeoreferenced(self.context).geo).wkt
     except (ValueError, TypeError, NotImplementedError):
         # context is not a valid shape.
         pass
     return u''
 def wkt(self):
     try:
         from shapely.geometry.geo import asShape
     except ImportError:
         from pygeoif.geometry import as_shape as asShape
     try:
         return asShape(IGeoreferenced(self.context).geo).wkt
     except (ValueError, TypeError, NotImplementedError):
         # context is not a valid shape.
         pass
     return u''
Ejemplo n.º 9
0
    def geocode(self):
        # The geocoder works from either a name or a point.
        location = request.GET.get('location')
        bbox = simplejson.loads(request.GET.get('bbox','false'))
        if location is None and not bbox:
            abort(400)

        geoc = geocoders.Google(g.map_key, output_format='json')

        name_based = False
        if not bbox:
            # We don't have a point, so we work with the name...
            name_based = True
            try:
                result = GeocoderController._result_with_locality(geoc.geocode(location, exactly_one=False))
                if not result:
                    return {}
                if not result.locality or not result.administrative:
                    # We want the geocoder to give us an canonical name for
                    # this location.  If it hasn't (because the user searched
                    # using a name that doesn't match up to the canonical name,
                    # we use the location to find the canonical name.
                    result = GeocoderController._result_with_locality(geoc.geocode('%f, %f' % (result.latitude, result.longitude), exactly_one=False))
                place, (lat, lng) = result
            except ValueError:
                return {}
        else:
            bbox = asShape(bbox)
            try:
                result = GeocoderController._result_with_locality(geoc.geocode('%f, %f' % (bbox.centroid.y, bbox.centroid.x), exactly_one=False))
                place, (lat, lng) = result
            except ValueError:
                return {}
        if result.locality and result.administrative:
            authoritative_name = '%s, %s' % (result.locality, result.administrative)
        else:
            authoritative_name = None
        try:
            meta.Session.query(Almanac).filter(Almanac.name==authoritative_name).one()
            almanac = True
        except exc.NoResultFound:
            almanac = False
        geopoint = Point(lng, lat)
        if bbox:
            c.almanacs = self._nearby_almanacs(bbox)
        else:
            c.almanacs = meta.Session.query(Almanac).join(Almanac.pages).distinct().filter(func.st_dwithin(Almanac.location, func.st_transform(func.st_geomfromtext('SRID=%s;POINT(%f %f)' % ('4326', result.longitude, result.latitude)), storage_SRID), 6233)).limit(10).all()
        nearby_kml = render('/almanac/kml.mako')
        return dict(lat=lat, lng=lng, layer=nearby_kml,
                    geojson=simplejson.dumps(geopoint.__geo_interface__),
                    authoritative_name=authoritative_name,
                    name_based=name_based,
                    almanac=almanac)
Ejemplo n.º 10
0
 def coordinates(self):
     geo_adapter = queryAdapter(self.context, IGeoreferenced)
     if geo_adapter:
         try:
             from shapely.geometry.geo import asShape
         except ImportError:
             from pygeoif.geometry import as_shape as asShape
         try:
             return asShape(IGeoreferenced(self.context).geo).wkt
         except (ValueError, TypeError, NotImplementedError):
             # context is not a valid shape.
             # create a validator?
             pass
     return u''
Ejemplo n.º 11
0
    def _do_edit_form_map(self, media_id):
        c.map = h.get_media_by_id(media_id)
        json = request.POST.get('feature')
        if json is None:
            abort(400)
        shape = simplejson.loads(json)
        # Stupid asShape returns a PointAdapter instead of a Point.  We round
        # trip it through wkb to get the correct type.
        location = wkb.loads(asShape(shape).to_wkb())

        location.srid = 4326

        c.map.location = location
        meta.Session.commit()

        c.editable = True
        return dict(html=render('/media/map/item.mako'),
                    map_id='pagemedia_%d' % c.map.order,
                    geometry=json,
                    )
Ejemplo n.º 12
0
    def _do_create(self):
        name = self.form_result['name']

        # Prevent creation of duplicates
        try:
            almanac = meta.Session.query(Almanac).filter(Almanac.name==name).one()
            return redirect_to(h.url_for('page_create', almanac_slug=almanac.slug))
        except exc.NoResultFound:
            pass

        json = self.form_result['almanac_center']
        shape = simplejson.loads(json)
        # We've requested a LonLat from OpenLayers, so it gives us a point in
        # Plate Carree (4326)
        point = asShape(shape)
        point.srid = 4326
        slug = Almanac.name_almanac(name)
        almanac = Almanac(name, slug)
        almanac.location = point

        meta.Session.save(almanac)
        meta.Session.commit()

        redirect_to(h.url_for('page_create', almanac_slug=slug))
Ejemplo n.º 13
0
    path = '/home/bkoziol/git/OpenClimateGIS/bin/geojson/watersheds_4326.geojson'
    #    select = ['HURON']
    select = []
    with open(path, 'r') as f:
        data = ''.join(f.readlines())
#        data2 = f.read()
    gj = geojson.loads(data)
    POLYINT = []
    for feature in gj['features']:
        if select:
            prop = feature['properties']
            if prop['HUCNAME'] in select:
                pass
            else:
                continue
        geom = asShape(feature['geometry'])
        if not isinstance(geom, MultiPolygonAdapter):
            geom = [geom]
        for polygon in geom:
            POLYINT.append(polygon)

    TEMPORAL = [datetime.datetime(1950, 2, 1), datetime.datetime(1950, 4, 30)]
    #    TEMPORAL = [datetime.datetime(1950,2,1),datetime.datetime(1950,3,1)]
    DISSOLVE = True
    CLIP = True
    VAR = 'Prcp'

    dataset = nc.Dataset(NC, 'r')

    if type(POLYINT) not in (list, tuple): POLYINT = [POLYINT]
Ejemplo n.º 14
0
        def geomatch(tag, subdict):
            # optimize if self is points or has more items
            if self.reader.shapeType in (pyshp.POINT, pyshp.MULTIPOINT) or len(self.reader) > len(subdict["refdata"].reader):
                # create spindex
                if not hasattr(self, "spindex"):
                    self.spindex = rtree.index.Index()
                    for i,shp in enumerate(self.reader.iterShapes()):
                        bbox = [shp.points[0][0],shp.points[0][1],shp.points[0][0],shp.points[0][1]] if shp.shapeType == pyshp.POINT else shp.bbox
                        self.spindex.insert(i, bbox)

                # first reverse matching for speed
                def revmatches():
                    for row,shp in izip(subdict["refdata"].reader.iterRecords(), subdict["refdata"].reader.iterShapes()):
                        #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
                        #print "...",row[1] #row[4],row[14]
                        print str(row)[:100]
                        prepped = prep(asShape(shp)) # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)
                        bbox = [shp.points[0][0],shp.points[0][1],shp.points[0][0],shp.points[0][1]] if shp.shapeType == pyshp.POINT else shp.bbox
                        ilist = self.spindex.intersection(bbox)
                        for i in ilist:
                            othershp = self.reader.shape(i)
                            if prepped.intersects(asShape(othershp)):
                                yield row,i

                lookups = dict()
                key = lambda(row,i): i
                for i,items in groupby(sorted(revmatches(), key=key), key=key):
                    names = []
                    for matchrow,i in items:
                        name = dict(zip(subdict["refdata"].fields, matchrow))[subdict["namefield"]]
                        name = name.decode(subdict["refdata"].encoding) if isinstance(name, basestring) else name
                        names.append(name)

                    matches = "|".join(names)
                    lookups[i] = matches

                for i,row in enumerate(self.reader.iterRecords()):
                    matches = lookups.get(i, None)
                    print i, matches
                    yield matches

            else:
                # create spindex
                subdict["spindex"] = rtree.index.Index()
                for i,shp in enumerate(subdict["refdata"].reader.iterShapes()):
                    bbox = [shp.points[0][0],shp.points[0][1],shp.points[0][0],shp.points[0][1]] if shp.shapeType == pyshp.POINT else shp.bbox
                    subdict["spindex"].insert(i, bbox)

                # match each
                for row,shp in izip(self.reader.iterRecords(), self.reader.iterShapes()):
                    #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
                    #print "...",row[1] #row[4],row[14]
                    prepped = prep(asShape(shp)) # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)
                    
                    subdict["matches"] = []
                    refdata = subdict["refdata"]
                    namefield = subdict["namefield"]
                    spindex = subdict["spindex"]
                    bbox = [shp.points[0][0],shp.points[0][1],shp.points[0][0],shp.points[0][1]] if shp.shapeType == pyshp.POINT else shp.bbox
                    ilist = spindex.intersection(bbox)
                    for i in ilist:
                        othershp = refdata.reader.shape(i)
                        #for otherrow,othershp in zip(refdata.reader.iterRecords(), refdata.reader.iterShapes()):
                        if prepped.intersects(asShape(othershp)):
                            otherrow = refdata.reader.record(i)
                            otherrow = rowdecode(otherrow)
                            name = dict(zip(refdata.fields, otherrow))[namefield]
                            subdict["matches"].append(name)
                    subdict["matches"] = "|".join(subdict["matches"])

                    print row, subdict["matches"]

                    yield subdict["matches"]
Ejemplo n.º 15
0
    path = '/home/bkoziol/git/OpenClimateGIS/bin/geojson/watersheds_4326.geojson'
#    select = ['HURON']
    select = []
    with open(path,'r') as f:
        data = ''.join(f.readlines())
#        data2 = f.read()
    gj = geojson.loads(data)
    POLYINT = []
    for feature in gj['features']:
        if select:
            prop = feature['properties']
            if prop['HUCNAME'] in select:
                pass
            else:
                continue
        geom = asShape(feature['geometry'])
        if not isinstance(geom,MultiPolygonAdapter):
            geom = [geom]
        for polygon in geom:
            POLYINT.append(polygon)
    
    TEMPORAL = [datetime.datetime(1950,2,1),datetime.datetime(1950,4,30)]
#    TEMPORAL = [datetime.datetime(1950,2,1),datetime.datetime(1950,3,1)]
    DISSOLVE = True
    CLIP = True
    VAR = 'Prcp'

    dataset = nc.Dataset(NC,'r')
    
    if type(POLYINT) not in (list,tuple): POLYINT = [POLYINT]
        
Ejemplo n.º 16
0
        def geomatch(tag, subdict):
            # optimize if self is points or has more items
            if self.reader.shapeType in (pyshp.POINT, pyshp.MULTIPOINT) or len(
                    self.reader) > len(subdict["refdata"].reader):
                # create spindex
                if not hasattr(self, "spindex"):
                    self.spindex = rtree.index.Index()
                    for i, shp in enumerate(self.reader.iterShapes()):
                        bbox = [
                            shp.points[0][0], shp.points[0][1],
                            shp.points[0][0], shp.points[0][1]
                        ] if shp.shapeType == pyshp.POINT else shp.bbox
                        self.spindex.insert(i, bbox)

                # first reverse matching for speed
                def revmatches():
                    for row, shp in izip(
                            subdict["refdata"].reader.iterRecords(),
                            subdict["refdata"].reader.iterShapes()):
                        #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
                        #print "...",row[1] #row[4],row[14]
                        print str(row)[:100]
                        prepped = prep(
                            asShape(shp)
                        )  # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)
                        bbox = [
                            shp.points[0][0], shp.points[0][1],
                            shp.points[0][0], shp.points[0][1]
                        ] if shp.shapeType == pyshp.POINT else shp.bbox
                        ilist = self.spindex.intersection(bbox)
                        for i in ilist:
                            othershp = self.reader.shape(i)
                            if prepped.intersects(asShape(othershp)):
                                yield row, i

                lookups = dict()
                key = lambda (row, i): i
                for i, items in groupby(sorted(revmatches(), key=key),
                                        key=key):
                    names = []
                    for matchrow, i in items:
                        name = dict(zip(subdict["refdata"].fields,
                                        matchrow))[subdict["namefield"]]
                        name = name.decode(
                            subdict["refdata"].encoding) if isinstance(
                                name, basestring) else name
                        names.append(name)

                    matches = "|".join(names)
                    lookups[i] = matches

                for i, row in enumerate(self.reader.iterRecords()):
                    matches = lookups.get(i, None)
                    print i, matches
                    yield matches

            else:
                # create spindex
                subdict["spindex"] = rtree.index.Index()
                for i, shp in enumerate(
                        subdict["refdata"].reader.iterShapes()):
                    bbox = [
                        shp.points[0][0], shp.points[0][1], shp.points[0][0],
                        shp.points[0][1]
                    ] if shp.shapeType == pyshp.POINT else shp.bbox
                    subdict["spindex"].insert(i, bbox)

                # match each
                for row, shp in izip(self.reader.iterRecords(),
                                     self.reader.iterShapes()):
                    #if dict(zip(self.fields, row))["SOV0NAME"] != "Russia": continue
                    #print "...",row[1] #row[4],row[14]
                    prepped = prep(
                        asShape(shp)
                    )  # prepares geometry for many intersection tests (maybe only useful if is polygon and other is points, but not sure)

                    subdict["matches"] = []
                    refdata = subdict["refdata"]
                    namefield = subdict["namefield"]
                    spindex = subdict["spindex"]
                    bbox = [
                        shp.points[0][0], shp.points[0][1], shp.points[0][0],
                        shp.points[0][1]
                    ] if shp.shapeType == pyshp.POINT else shp.bbox
                    ilist = spindex.intersection(bbox)
                    for i in ilist:
                        othershp = refdata.reader.shape(i)
                        #for otherrow,othershp in zip(refdata.reader.iterRecords(), refdata.reader.iterShapes()):
                        if prepped.intersects(asShape(othershp)):
                            otherrow = refdata.reader.record(i)
                            otherrow = rowdecode(otherrow)
                            name = dict(zip(refdata.fields,
                                            otherrow))[namefield]
                            subdict["matches"].append(name)
                    subdict["matches"] = "|".join(subdict["matches"])

                    print row, subdict["matches"]

                    yield subdict["matches"]