WP_List_Table and an Extra Nonce

I am working on a WordPress plugin. For part of it I am adding a Metabox onto the Post Edit screen in the admin section. However things stop working if I add a WP_List_Table. That is because by default the list table adds a _wpnonce field, even though there is already one there for the Edit page.

This of course messes things up and prevents you from saving or updating. Not awesome. The field is output in the display_tablenav() function in the base list_table class. It looks like there has been a fix submitted, lets hope it gets into an upcoming version.

Until then, you can simply override the display_tablenav() function with a blank function or one that provides similar functionality.

Tilestream: Fixing interaction

When I upgrade to a new version of Tilestream / Wax, apparently the method for adding interactivity change. Before I think it pulled some of the info from a JSON file. It can still do this, but my files seemed to include localhost versions of the server. Anyhow, long story short, you need to make sure you specify a template in your TileJSON. To find this, request the TileJSON Tilestream serves up for your made:

http://server/v2/layer_name.json

Now look for the template variable and add it into the TileJSON you define in your javascript file. Mine is:

template: ‘{{#__location__}}{{/__location__}}{{#__teaser__}}{{{name}}} </br>\n{{{address}}}{{/__teaser__}}{{#__full__}}{{{permit_id}}}{{/__full__}}’,

Tilestream: Tiles not found

I just upgraded to a newer version of Tilestream. Unfortunately after doing that, all my maps stopped working. When I went to my Tilestream UI, I was able to see that it was up and it was finding the MBTILE files. When I clicked on info, it became clear that the URL had changed. Instead of having it be server/1.0.0/map it was now server/v2/map.

However after fixing my URLs, it still wasn’t working. Turns out I also needed to switch the mapping scheme from TMS to XYZ in the tilejson. Definitely not a seamless upgrade and I didn’t really see this change documented anywhere. I am still trying to figure out what I need to change to make interactivity work.

http://support.mapbox.com/discussions/tilestream/59-tiles-not-displaying-on-map

In your tilejson object, try changing the scheme to xyz instead of tms and let me know if that fixes the problem.

Installing NodePie on Ubuntu

I was transferring my latest creation over to my hosted Ubuntu server and came across the most annoying error. I had to reinstall the NodePie module because it relies on compiled C code. I kept getting an error though about something called lib-expat.
Anyhow, turns out you need to apt-get a new library:
sudo apt-get install libexpat1-dev

GitHub to the rescue:https://github.com/astro/node-xmpp/issues/33

Transferring from MongoDB to CouchDB

So I wanted to move a larger number of records from MongoDB to CouchDB. Here are the basic steps I took:

  1. I  just wanted a selected number of fields (name, address, longitude, latitude) so I used mongoexport to put it into a JSON Array:
    ./mongoexport -d cookography -c places -f name,address,longitude,latitude -o test.json –jsonArray
  2. Both CouchDB & MongoDB use _ID and MongoExport insists on adding it. However it does it in a way the CouchDB hates so I munge it:
    sed ‘s/_id/id/g’ test.json > tested.json
  3. Now you have to format your JSON Array so you can feed it the the REST interface of CouchDB. First add this to the beginning:
    (echo ‘{“docs”:’; cat tested.json) > output.json
    And then this to the end:
    echo “}” >> output.json
  4. Now send this to REST API for CouchDB:DB=”http://127.0.0.1:5984/places”

    curl -d @output.json -X POST $DB/_bulk_docs  -H “Content-Type: application/json”

a DIV inside a LI

Suppose you want to have a bunches of DIVs contained inside of a List Item… AND you want them to not mess with the height of the List Item and appear as a line of text?

Well then, take this sample HTML:

<li>
<div class="inspection">
<div class="date"><a href="/report/1430/9115/">9-16-2010</a></div>
<div class="inspection-type">License Renewal </div>
<div class="violations">0 Violations</div>
</div>
</li>

And apply this CSS:

.inspection {

overflow: hidden;

vertical-align: middle;

display: inline-block;

}

li {

margin-left: 15px;

list-style-type: disc;

list-style-position: inside;

}

PostGres – PostGIS on OSX Lion

I recently just wasted a lot of time trying to get PostGIS up and running on OSX Lion. I wanted to do this so I could pull in Open Street Map data for the DC region and then play with it in QGIS and import it into Tilemill. I will be the first to admit that this is a rather complex toolchain, but it shouldn’t have been as difficult as it was.

On the off chance I can save someone time, I will recap what I did… and some of the misstep.

My big misstep was trying to use the precompiled binaries from KyngChaos. While they work fine, the OSM importer for PostGIS always SegFaulted when I tried running it against these versions. I even tried compiling the importer from source code and couldn’t get it work.

Here is what to do:

  1. Use Homebrew. It makes it easy to download and compile source code. You may need to get rid of old cruft you have previously downloaded. I had to remove MacPython and some old Frameworks. If you run “brew doctor” it will give you hints.
  2. Install Homebrew
  3. Install PostGres, PostGIS and all dependancies: ‘brew install postgis’
  4. Initialize the database. This will create a Database structure and a database user (“role”) that is the same as user Mac user name: ‘initdb /usr/local/var/postgres/’
  5. Create a database for GIS stuff (Instead of Luke, use your OSX username): ‘createdb -U Luke -E utf8 gis’
  6. Start the PostGres server – for some reason I needed to specify that it is running on localhost: ‘postgres -D /usr/local/var/postgres/ -h localhost’
  7. Add the PostGIS stuff to the DB: ‘psql -d gis -U Luke -f /usr/local/share/postgis/postgis.sql’ and also ‘psql -d gis -U Luke -f /usr/local/share/postgis/spatial_ref_sys.sql’
  8. Awesome! Now you should have a PostGIS DB loaded up. Now time to install the importer: ‘brew install osm2pgsql’
  9. It is going to fail. This is because you need to apply a patch to fix some inconsistency. Big pain in the ass. To do that, download the patch from here: http://trac.openstreetmap.org/raw-attachment/ticket/3299/patch-build_geometry.cpp
  10. To apply the patch, use the patch command: ‘cd ~/Library/Caches/Homebrew/osm2pgsql–svn/’ and then ‘patch -p0 < ~/Downloads/patch-build_geometry.cpp’
  11. Now try it again and it should work: ‘brew install osm2pgsql’
  12. Awesome, almost there. Next step is to download some OSM data. Cloudmade makes it pretty easy. I grabbed the DC data from here: http://downloads.cloudmade.com/americas/northern_america/united_states/district_of_columbia#downloads_breadcrumbs
  13. Goto where ever you downloaded your files to and run (replacing it with your OSM file): ‘osm2pgsql -U Luke -H localhost -d gis  district_of_columbia.osm’
  14. TADA! All that wonderful data should be loaded into your PostGIS DB. Now open up QGIS and add in a PostGIS layer. Make sure you use your OSX Username as the username (Luke) and the PostGIS DB you just created (DB).

JSDOM Memory leaks

JSDOM is a great little module for NodeJS which lets you parse a DOM on the server. The only problem is that it has a memory leak. Not a big deal if you are only going to instantiate a couple times. A little trickier if you are screen scraping and need to call it 1000s of times. I luckily found a work around. Instead of creating a new window every time you want to parse some code, simply keep the same window around and switch what it is displaying:

From the google groups:

I’m basically doing this:

var jsdom = require(‘jsdom’);
var document = jsdom.jsdom(‘<html><body>’);
var window = document.createWindow();

var JQUERY_URL = ‘http://localhost/js/jquery-1.4.4.min.js’;

function foo(text) {
document.innerHTML = ‘<html><body>’ + text;
jsdom.jQueryify(window, JQUERY_URL, function() {
var $ = window.$;
console.log(document.innerHTML);
});

}

setInterval(function() {
foo(“Text”);

}, 1000);

And the process’ memory increases and eventually terminates. How do
you suggest I should reuse the DOM?

I am trying this for now:

function foo(text) {
document.innerHTML = ‘<html><body>’ + text;
if (!window.$) {
jsdom.jQueryify(window, JQUERY_URL, function() {
var $ = window.$;
console.log(document.innerHTML);
});
} else {
var $ = window.$;
console.log(document.innerHTML);
}

}