Permalink
Browse files

initial commit, things work

  • Loading branch information...
0 parents commit d210329a779b863571a2615a46801ee306ced751 @eklitzke committed Jan 16, 2009
Showing with 2,350 additions and 0 deletions.
  1. +13 −0 LICENSE
  2. +1 −0 README
  3. +2,228 −0 data.h
  4. +85 −0 geoquad.c
  5. +23 −0 setup.py
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2008, Evan Klitzke <evan@eklitzke.org>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
1 README
@@ -0,0 +1 @@
+C module for doing fast geoquad operations.
2,228 data.h
2,228 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
85 geoquad.c
@@ -0,0 +1,85 @@
+#include <Python.h>
+
+#include "data.h"
+#include <stdio.h>
+#include <stdint.h>
+
+#include <stdlib.h>
+
+#define LONGITUDE_MIN -180.0
+#define LONGITUDE_MAX 180.0
+#define LATITUDE_MIN -90.0
+#define LATITUDE_MAX 90.0
+#define GEOQUAD_STEP 0.05
+
+static inline uint32_t interleave32(uint16_t x, uint16_t y)
+{
+ return (
+ morton_forward[y >> 8] << 17 |
+ morton_forward[x >> 8] << 16 |
+ morton_forward[y & 0xFF] << 1 |
+ morton_forward[x & 0xFF]);
+}
+
+static inline void deinterleave32(uint32_t z, uint16_t *x, uint16_t *y)
+{
+ uint16_t half = z & 0xFFFF;
+ *x = morton_sparse[half & 0x5555];
+ half >>= 1;
+ *y = (morton_sparse[half & 0x5555]);
+ half = z >> 16;
+ *x |= (morton_sparse[half & 0x5555]) << 8;
+ half >>= 1;
+ *y |= ((morton_sparse[half & 0x5555]) << 8);
+}
+
+static PyObject*
+geoquad_create(PyObject *self, PyObject *args)
+{
+ uint16_t i, j;
+ uint32_t result;
+ float lng, lat;
+ if (!PyArg_ParseTuple(args, "ff", &lng, &lat))
+ return NULL;
+ i = (uint16_t) ((lng - LONGITUDE_MIN) / GEOQUAD_STEP);
+ j = (uint16_t) ((lat - LATITUDE_MIN) / GEOQUAD_STEP);
+
+ /* yes this is backwards. don't ask */
+ result = interleave32(j, i);
+ return PyInt_FromLong((long) result);
+}
+
+static PyObject*
+geoquad_parse(PyObject *self, PyObject *args)
+{
+ uint16_t i, j;
+ float lng, lat;
+ long geoquad;
+ PyObject *ret;
+
+ if (!PyArg_ParseTuple(args, "l", &geoquad))
+ return NULL;
+
+ if ((ret = PyTuple_New(2)) == NULL)
+ return NULL;
+
+ deinterleave32((uint32_t) geoquad, &i, &j);
+ lat = (float) ((i * GEOQUAD_STEP) + LATITUDE_MIN);
+ lng = (float) ((j * GEOQUAD_STEP) + LONGITUDE_MIN);
+
+ PyTuple_SetItem(ret, 0, PyFloat_FromDouble((double) lng));
+ PyTuple_SetItem(ret, 1, PyFloat_FromDouble((double) lat));
+ return ret;
+}
+
+static PyMethodDef geoquad_methods[] = {
+ { "create", (PyCFunction) geoquad_create, METH_VARARGS, "create a geoquad from a (lng, lat)" },
+ { "parse", (PyCFunction) geoquad_parse, METH_VARARGS, "parse a geoquad, returns a (lng, lat)" },
+ { NULL }
+};
+
+PyMODINIT_FUNC initgeoquad(void)
+{
+ PyObject *m = Py_InitModule3("geoquad", geoquad_methods, "test");
+}
+/* vim: set ts=4 sw=4 tw=78 noet: */
23 setup.py
@@ -0,0 +1,23 @@
+#!/usr/bin/python
+
+from distutils.core import setup, Extension
+import os
+
+__version__ = '0.1'
+
+macros = [('MODULE_VERSION', '"%s"' % __version__)]
+
+geoquad_extension = Extension(
+ name='geoquad',
+ sources=['geoquad.c'],
+ define_macros=macros,
+)
+
+setup(
+ name = 'geoquad',
+ version = __version__,
+ author = 'Evan Klitzke',
+ author_email = 'evan@eklitzke.org',
+ description = 'Cogent in-process',
+ ext_modules = [geoquad_extension]
+)

0 comments on commit d210329

Please sign in to comment.