manipulate n insert field values

78 views
Skip to first unread message

Shameema Mohsin

unread,
May 2, 2016, 7:13:39 AM5/2/16
to Django users
i have 2 fields latitude n longitude, and need to calculate z order value of lat n long n insert to a field

Shameema Mohsin

unread,
May 2, 2016, 9:38:53 AM5/2/16
to Django users
Could anybody help me out as I am new to Django.
In my model:

from django.db import models

class Employees(models.Model):
empId = models.AutoField(primary_key=True)
empName = models.CharField(max_length=200)
empEmail = models.EmailField()
empMob = models.CharField(max_length=200)
empAddr = models.TextField()
empLat = models.DecimalField(max_digits=9,decimal_places=6)
empLong = models.DecimalField(max_digits=9,decimal_places=6)
empDOB = models.DateField()
zval = models.IntegerField(default=0)

def __str__(self):
return self.empName

In the z value field i need to insert the z curve value calculated from latitude and longitude. where should I write the code? In models.py or somewhere else?

Erik Cederstrand

unread,
May 2, 2016, 11:40:12 AM5/2/16
to Django Users
There are two strategies here. If zval is always generated from empLat and empLong, then you could change zval from a field to a property on the Employees class. Otherwise, you risk zval falling out of sync if you are not careful to always re-calculate zval when empLat/empLong changes:

class Employees(models.Model):
[...]
@property
def zval(self):
return # your calculation here


The other strategy would be to place the calculation in the save() method:

class Employees(models.Model):
[...]
def save(self, *args, **kwargs):
self.zval = # your calculation here
super().save(*args, **kwargs)

Beware that save() is not always called. For example, bulk_create() doesn't call save(). You need to handle these situations yourself.

Erik

Shameema Mohsin

unread,
May 2, 2016, 7:44:58 PM5/2/16
to Django users
Thank you Erik. I will try the property method.

Shameema Mohsin

unread,
May 2, 2016, 8:45:48 PM5/2/16
to Django users

Stil stuck with the z order calculation and usage of the property field. Kindly help. I am new to Django, only knows php well.

Erik Cederstrand

unread,
May 3, 2016, 5:52:46 AM5/3/16
to Django Users

> Den 3. maj 2016 kl. 02.45 skrev Shameema Mohsin <itz.sh...@gmail.com>:
>
>
> Stil stuck with the z order calculation and usage of the property field. Kindly help. I am new to Django, only knows php well.

Where are you stuck? Show your error messages, current non-working code and expected result, or describe your problem in more detail.

I assume you have code to calculate the z order (I have no idea what it is). Otherwise, other forums would be better for help on this. Assuming that, you would do:

def get_z_order(lat, long):
# Jiggle those numbers
return z_order


class Employees(models.Model):
[...]
@property
def zval(self):
return get_z_order(self.empLat, self.empLong)


You can then access zval as if it was a model field.

Erik

Shameema Mohsin

unread,
May 3, 2016, 9:05:11 PM5/3/16
to django...@googlegroups.com
#Z value calculation
#................................
#step1: converting lat and long to int

latitude int = (latitude + 90) × 10^6
longitude int = (latitude + 180) × 10^6

#note 10^6 = 1000000

We compute the Morton value for a spatial point P (x, y)
by interleaving the bits of x and y. For example, when x =
3(011 ) and y = 4(100), the Morton value for P is
100101 = 37

#step2: convert int (base 10) to binary digits (base 2)
..........?
#step3: interleave both values bits and compute z value
#take first bit from y cordinate  (longitude) : 1
#take first bit from x cordinate  (lat) : 0
#take 2nd bit from y cordinate  (longitude) :0
#take 2nd bit from x cordinate  (lat) : 1
#take 3rd bit from y cordinate  (longitude) : 0
#take 3rd bit from x cordinate  (lat) : 1

therefore z binary value is 100101
and z value is 37

#this is the technique used to convert spatial 2D indexing to 1D to improve efficiency.

I do not know the python syntax. Kindly help.






--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8IzLt_0cO8Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0E25E884-F38C-4314-B2B3-0ADAF813253E%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Shameema Mohsin

unread,
May 4, 2016, 4:07:59 AM5/4/16
to Django users

Z value calculation
.............................
...
step1: converting lat and long to int

latitude int = (latitude + 90) × 10^6
longitude int = (latitude + 180) × 10^6

note 10^6 = 1000000

We compute the Morton value for a spatial point P (x, y)
by interleaving the bits of x and y. For example, when x =
3(011 ) and y = 4(100), the Morton value for P is
100101 = 37

step2: convert int (base 10) to binary digits (base 2)
..........?
step3: interleave both values bits and compute z value
take first bit from y cordinate  (longitude) : 1
take first bit from x cordinate  (lat) : 0
take 2nd bit from y cordinate  (longitude) :0
take 2nd bit from x cordinate  (lat) : 1
#take 3rd bit from y cordinate  (longitude) : 0
#take 3rd bit from x cordinate  (lat) : 1

Erik Cederstrand

unread,
May 4, 2016, 4:40:30 AM5/4/16
to Django Users
I'm sorry, but asking for a full Python implementation of the above is too much to ask for free help for. Implement it in PHP if that's what you're familiar with, and grab a Python handbook to look up the equivalent Python syntax.

Erik

Shameema Mohsin

unread,
May 4, 2016, 10:15:18 AM5/4/16
to Django users
Thanks Erik. Doing so.

Peter of the Norse

unread,
Jun 5, 2016, 2:06:53 AM6/5/16
to django...@googlegroups.com
I found this: https://github.com/trevorprater/pymorton

The problem is I’ve only done one search for Z curve, and I found three different ways to do it for lat/long. Since there’s no standard implementation, there’s very little point to it. If you are doing it to make it easier to search, then you can just create an index on (lat, long).
Peter of the Norse
Rahm...@Radio1190.org



Reply all
Reply to author
Forward
0 new messages