Enrico Zini: Searching OSM nodes in Spatialite
Searching OSM nodes in Spatialite
Third step of my SoTM10 pet project: finding the POIs.
I put together a query to find all nodes with a given tag inside a bounding
box, and also a query to find all the tag values for a given tag name inside a
bounding box.
The result is this simple POI search engine:
# # poisearch - simple geographical POI search engine # # Copyright (C) 2010 Enrico Zini <enrico@enricozini.org> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # from pysqlite2 import dbapi2 as sqlite class PoiDB(object): def __init__(self): self.db = sqlite.connect("pois.db") self.db.enable_load_extension(True) self.db.execute("SELECT load_extension('libspatialite.so')") self.oldsearch = [] self.bbox = None def set_bbox(self, xmin, xmax, ymin, ymax): '''Set bbox for searches''' self.bbox = (xmin, xmax, ymin, ymax) def tagid(self, name, val): '''Get the database ID for a tag''' c = self.db.cursor() c.execute("SELECT id FROM tag WHERE name=? AND value=?", (name, val)) res = None for row in c: res = row[0] return res def tagnames(self): '''Get all tag names''' c = self.db.cursor() c.execute("SELECT DISTINCT name FROM tag ORDER BY name") for row in c: yield row[0] def tagvalues(self, name, use_bbox=False): ''' Get all tag values for a given tag name, optionally in the current bounding box ''' c = self.db.cursor() if self.bbox is None or not use_bbox: c.execute("SELECT DISTINCT value FROM tag WHERE name=? ORDER BY value", (name,)) else: c.execute("SELECT DISTINCT tag.value FROM poi, poitag, tag" " WHERE poi.rowid IN (SELECT pkid FROM idx_poi_geom WHERE (" " xmin >= ? AND xmax <= ? AND ymin >= ? AND ymax <= ?) )" " AND poitag.tag = tag.id AND poitag.poi = poi.id" " AND tag.name=?", self.bbox + (name,)) for row in c: yield row[0] def search(self, name, val): '''Get all name:val tags in the current bounding box''' # First resolve the tagid tagid = self.tagid(name, val) if tagid is None: return c = self.db.cursor() c.execute("SELECT poi.name, poi.data, X(poi.geom), Y(poi.geom) FROM poi, poitag" " WHERE poi.rowid IN (SELECT pkid FROM idx_poi_geom WHERE (" " xmin >= ? AND xmax <= ? AND ymin >= ? AND ymax <= ?) )" " AND poitag.tag = ? AND poitag.poi = poi.id", self.bbox + (tagid,)) self.oldsearch = [] for row in c: self.oldsearch.append(row) yield row[0], simplejson.loads(row[1]), row[2], row[3] def count(self, name, val): '''Count all name:val tags in the current bounding box''' # First resolve the tagid tagid = self.tagid(name, val) if tagid is None: return c = self.db.cursor() c.execute("SELECT COUNT(*) FROM poi, poitag" " WHERE poi.rowid IN (SELECT pkid FROM idx_poi_geom WHERE (" " xmin >= ? AND xmax <= ? AND ymin >= ? AND ymax <= ?) )" " AND poitag.tag = ? AND poitag.poi = poi.id", self.bbox + (tagid,)) for row in c: return row[0] def replay(self): for row in self.oldsearch: yield row[0], simplejson.loads(row[1]), row[2], row[3]Problem 3 solved: now on to the next step, building a user interface for it.




Currently I own and use an iPhone 3G. I bought it almost two years ago, when the local phone provider LMT started offering the iPhone legally. I had a pretty good experience with it most of the time, but now it is showing its age:
Last weekend (2010-07-02 2010-07-04) nine people met at the FAI developer workshop at 


Some time ago, someone posted to a development mailing list about
a piece of code, Cc:ing some people who had modified the code in
recent past. Within hours, the original author replied to complain
about not being Cc'd.
Several thoughts went through my mind:
Today, virtually all of the free software available can be found in packaged form in distributions like Debian and Ubuntu. Users of these distributions have access to a library of thousands of applications, ranging from trivial to highly sophisticated software systems. Developers can find a vast array of programming languages, tools and libraries for constructing new applications.
This is possible because we have a mature
My girlfriend and I were out picking up a cake at the bakery last week, when my phone rang:


I was planning to attend
I hate keeping track of money. Bank accounts, credit cards,
investing it's such a hassle. Hell, sometimes I even hate the fact
that money exists and needs to be dealt with in the first place. But I
admit that it's an easier system than bartering for everything.
In college I pretty much ignored most things financial. I rarely had
more money than I needed, and put little financial planning into
deciding what I could and could not spend money on besides, "I'm going
to make X dollars this summer," and, "think twice." But now that I
have a steady paycheck, it seems like a good idea to know where all my
money goes, so I can make better decisions about how I'm spending (and
saving) it.
But I've kept procrastinating starting to do so, because GUI programs
like
At 
Now that disk space is cheap, it makes more and more sense to rip
music and store it in FLAC format. I used to use abcde to encode
directly to Ogg Vorbis, but a while back I decided to re-rip. The
disks in the server at home now have lots of hours of music in a nice
lossless format ready for easy playback on various machines on the
network.
However, that's not all I need. I also want to be able to create
Ogg Vorbis files for my own music player with limited space. And Jo's
Ipod won't play either .ogg or .flac (yay for closed devices!)... At
least I now have readily-available original quality music already on
hard disk, so I don't need to go and find CDs again. But I really
don't want to have to work through my directories of music by hand,
re-encoding individually.
My solution: write a filesystem that will read in the .flac files
and convert to .ogg or .mp3 on the fly. Now I can simply read the
files I want off the server (via sftp/rsync/whatever) and it will
create them for me!
So, fuse-music is my first attempt to do this. It's a simple perl
program that works using the perl FUSE bindings to generate a
pseudo-filesystem where a tree of .flac files will appear as a tree of
.ogg or .mp3 files instead. It's not wonderfully fast (as encoding
takes time), but it works. The only major difficulty I have at this
point is calculating the sizes of the output files: if I want rsync to
work reliably then the reported sizes need to be static and
correct. But it's not possible (AFAICS) to predict the size of an MP3
or Ogg Vorbis file without actually doing the encoding and
measuring the output size. So (ick!) that's what the code currently
does when necessary. It caches the output size in a perl hash DB so
the work is only needed once per file; ideally I'd like to not to have
to do this at all!
If this sounds interesting at all, feel free to have a look at and
play with the
code: