Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Build geographical grids in Python
This is a Python library implementing the "carroyage" for geographical data.
In order to be able to make analysis in the more granular level of the "carreau", this provides an easy reference to building custom grids by allowing to assign a unique tile / "carreau" ID to a geographical coordinate (latitude, longitude) or a French postal address.
Indeed, the result of the computation gives the ID according to the EU directive INSPIRE in the following format:
WGS84|RES200m|N2471400|E0486123
that could be decomposed as a pipe-separated string with:
WGS84
;RES200m
for $200\text{ m}$;N2471400
for $\text{N }24.714000°$;E0486123
for $\text{E }4.861230°$;as well as the coordinate in the grid in the form of <row>.<column>
from the initial bottom-left point.
It would need as input parameters:
200m
, 1km
, ...;WGS84
).Below are the calculation bases.
In WGS84
, one degree of latitude is almost equivalent to $111\text{ km}$ everywhere on Earth.
The distance in meter, $D_{lat}$, for one degree in latitude is given by the following formula:
D_{lat} = \pi \cdot \frac{a (1 - e^2)}{(1 - e^2 \sin^2(\text{latitude}))^{3/2}} \times \frac{1}{180}
where
WGS84
, $a = 6378137$);e = \sqrt{1 - \frac{b^2}{a^2}}
(in WGS84
, $b = 6356752.3142$)
From here, one can compute the degree in latitude for any size of tile / "carreau", eg. in WGS84
, a "carreau" of height $200\text{ m} \approx 0.001796°$:
\Delta latitude = \frac{distance}{D_{lat}}
where
The variation of longitude as a function of latitude is calculated by taking into account the decrease in the distance between the meridians as one moves away from the equator. This decrease is due to the fact that the Earth is spherical.
In WGS84
, the formula that allows one to calculate the distance corresponding to a degree of longitude as a function of a latitude is the following:
\Delta{longitude} = \frac{distance}{D_{lon} \times \cos(latitude)}
where
D_\text{lon} = \pi \cdot a \cdot \cos(\text{latitude}) \cdot \frac{1}{180} \cdot \frac{1}{\sqrt{1 - e^2 \sin^2(\text{latitude})}}
The UTM projection would be used to handle XY coordinates with the central longitude $\lambda_0$ being automatically calculated for the UTM zone with the longitude of the searched point, eg. $-3°$ for the UTM zone 30T
covering France from $W 6°$ to $0°$.
The following data and formula would be used in that case: eg. for WGS84
One can now compute the XY coordinates as follows:
NB: If the searched point is in the south, add $10,000,000$ to $Y$ to avoid negative coordinates.
With all these formulas, one is now able to build a full system placing any GPS or XY coordinates into a unique "carreau". This library implements one way to do it.
$ git clone https://github.com/cyrildever/gridding.git
$ cd gridding/packages/py/
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install build twine
pip install gridding-py
If you want to use the from_address()
feature, you need to build or upload the address repository.
Your one-line address must be compliant with the following guidelines:
<numero> <repetitor> <voie> <lieu_dit> <cp> <ville>
;<numero>
is the street number;<repetitor>
is the eventual addition to the number, eg. bis
, ter
, ...;<voie>
is the street name including its type;<lieu_dit>
is a distinct locality (neither the city nor a postal box information);<cp>
is the postal code in a full integer format (2A000
must be transformed in 20000
for Corsica for example);<ville>
is the city name (ideally without further information, ie. no Arrondissement
or Cedex
mentions).For now, only French addresses are fully covered, but the library can work with any other country providing you build the corresponding repository.
Feel free to add any normalizing tools through pull requests in the library to help enriching it.
To get the tile / "carreau" from GPS coordinates:
from gridding import METER, WGS84, GPS, Grid, Resolution
grid = Grid(
Resolution(200, METER),
GPS(-5.151110, 41.316666),
WGS84(),
)
carreau, tile = grid.from_gps(my_point)
print(f"This GPS point belongs to the carreau with code: {carreau}, and coordinate: {tile.to_string()}")
To get it from a French postal address:
from gridding import FR
carreau, tile = grid.from_address("9 boulevard Gouvion Saint-Cyr 75017 Paris", FR())
print(f"This address belongs to the carreau with code: {carreau}, and coordinate: {tile.to_string()}")
To get it from X/Y coordinates:
from gridding import XY
my_point = XY(647872.07, 5110548.44, "UTM", "WGS84")
carreau, tile = grid.from_xy(my_point)
print(f"This X/Y point belongs to the carreau with code: {carreau}, and coordinate: {tile.to_string()}")
You may want to use the "distance" between two tiles (which returns a sort of average radius in tiles from one tile to the other):
distance = Tile.Distance(tile1, tile2)
print(f"The radius between these two tiles is equal to {distance} tiles")
The idea here is to be able to easily know if a tile is within a certain tile distance of another, eg. two tiles away in each direction.
For now, only the WGS-84
system is available and can be used in conjunction with the UTM
projection if need be to get X/Y coordinates.
IMPORTANT: when using negative coordinates for a pivot point, be sure to give it a 0
as 6th decimal.
You may also use the main script to get the "carreau" from some GPS coordinates directly in a terminal, eg.
usage: python -m gridding [-h] -x LONGITUDE -y LATITUDE -r RESOLUTION [-o | --obfuscate | --no-obfuscate] [-t | --tile | --no-tile]
options:
-h, --help show this help message and exit
-x LONGITUDE, --longitude LONGITUDE
the longitude in decimal degrees
-y LATITUDE, --latitude LATITUDE
the latitude in decimal degrees
-r RESOLUTION, --resolution RESOLUTION
the grid resolution, eg. '200m'
-o, --obfuscate, --no-obfuscate
add to return a hashed result (default: no)
-t, --tile, --no-tile
add to return the tile coordinates instead of the carreau id (default: no)
NB: The optional obfuscated result is a unique SHA-256 hexadecimal string.
$ pip install -e . && python3 -m unittest discover
This module is distributed under a MIT license.
See the LICENSE file.
FAQs
Build geographical grids
We found that gridding-py demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.