Convert Shapefile to SQL - sql

I'm trying to convert a shapefile I have to SQL format.
I've tried doing this using shp2pgsql, but, alas, this program doesn't read the SHAPEFILE.prj file, so I end up with coordinates in an inconvenient format.
Is there a way to convert shapefiles to SQL which respects their PRJ specification?

You may have things in one projection that you want to display or interact with in more familiar values, like longitude and latitude. For example Planet OpenStreetMap uses a spherical mercator and gives you values like this when you ask for text:
cal_osm=# select st_astext(way) from planet_osm_point limit 3;
st_astext
-------------------------------------------
POINT(-13852634.6464924 4928686.75004766)
POINT(-13850470.0501262 4930555.55031171)
POINT(-13850160.8268447 4930880.61375574)
(3 rows)
You can use st_transform to return a more familiar format like this:
cal_osm=# select st_astext(st_transform(way, 4326)) from planet_osm_point limit 3;
st_astext
-------------------------------------------
POINT(-124.440334282677 40.4304093953086)
POINT(-124.42088938268 40.4431868953078)
POINT(-124.418111582681 40.4454091953076)
(3 rows)

A prj file is essentially a text file that contains the coordinate system information in the ESRI Well-known-text(WKT) format. Could you just write a program that uses shp2pgsql to convert the geometries and then store the associated WKT string from the prj?
Of note: The WKT format is an EPSG accepted format for delimiting projected and geographic coordinate system information, but different authorities may have different names for projections or projection parameters. PostGIS might be different from Oracle which might in turn be different from ESRI. So if you store the prj's WKT make sure that it is in an esri_coordinate_system column. PostGIS might have a different naming convention format for parameters.
Also, in case you're interested, there is a C++ open FileGDB api that allows you to access row information without license. It's available in 32 and 64 bit on windows and linux:
http://resources.arcgis.com/content/geodatabases/10.0/file-gdb-api

Related

Unknown postgis gps data format

A third-party program stores tracking data to the db, but I not understand the format. I know that postgis is working there and this column should contain GPS location(s) and maybe additional data.
Example (db dump as csv):
"Location","DateTime"
"010100000023E37C4023E33C40417F41EF407F4740","2020-05-24 15:33:53+00"
How can I decode Location column data?
This is Well-known binary format.
See PostGIS methods for WKB: ST_AsBinary, ST_GeomFromWKB.
WKT methods: ST_AsText, ST_GeomFromText.
The example in WKT format: POINT(28.887256651392033 46.99416914651966).
For .Net can use Geo, NetTopologySuite.IO.TinyWKB.

Conversion of wkt to wkb in postgresql

I have a column named wkt_geometry in a table in Postgres with data converted from latitude and longitude. But I want to convert the wkt_geometry to wkb_geometry using sql commands.
If I convert it directly from Lat and long it would even better.
Also I have seen the ST_AsBinary(geometry) but I dont understand the parameters involved.
WKT and WKT are two formats that represent Geometry (or Geography) type.
To convert between various formats, you need to construct Geometry type and then export it to desired format. In this case ST_AsBinary(ST_GeomFromText(wkt_text)) should generally work.
If you want to produce WKB directly from lat/lng - something like ST_AsBinary(ST_MakePoint(lng, lat)) should work too. Pay attention to argument order, SQL functions use lng / lat order.

Storing and querying location (coords) in PostgreSQL

In my user schema I have a location column which will hold the last updated coordinates of the user. Is there a PostgreSQL data type I could use to allow me to run SQL queries to search, for example something like: SELECT username FROM Users WHERE currentLocation="20 Miles Away From $1"
If your "location" column holds GPS coordinates then basically your only option is to use the PostGIS extension. With PostGIS you store your locations in a column with geometry or geography data type and then you can apply a rich set of function on it. Your query would become something like:
SELECT username
FROM users
WHERE ST_Distance(currentlocation, $1) = 20; -- careful with units
This is assuming that $1 is of the same data type as currentlocation. You probably have to convert the miles to kilometers, depending on your coordinate reference system and the data type (geography would produce kilometers, geometryproduces whatever unit the data is in).

Bigquery SUM(Float_Values) returns multiple decimal places and Scientific Notation

I am trying to calculate Total Sales at a store. I have product Price in a column called UNIT_PRICE. All the prices have 2 decimal places example: 34.54 or 19.99 etc and they are imported as type:float in the schema. (UNIT_PRICE:float)
When I perform the select Query: "SELECT CompanyName, SUM(Unit_Price) as sumValue" etc I get the following returned in the column, but only "sometimes".
2.697829165015719E7
It should be something like: 26978291.65
As I am piping this out into spreadsheets and then charting it I need it to be in the type float or at least represent a normal price format.
I have tried the following but still having issues:
Source: Tried converting original data type to BigDecimal with only 2 decimal points in the source data and then exporting to the csv for import into bigquery but same result.
Bigquery: Tried converting to a string first and then to a float and then SUM but same result. "SELECT CompanyName, SUM(Float(String(Unit_Price))) as sumValue"
Any ideas on how to deal with this?
Thanks
BigQuery uses default formatting for floating point numbers, which means that depending on the size of the number, may use scientific notation. (See the %g format specifier here)
We tried switching this, but it turns out, it is hard to get a format that makes everyone happy. %f formatting always produces decimal format, but also pads decimals to a 6 digit precision, and drops decimals beyond a certain precision.
I've filed a bug to allow an arbitrary format string conversion function in BigQuery. It would let you run SELECT FORMAT_STRING("%08d", SUM(Unit_Price)) FROM ... in order to be able to control the exact format of the output.
Do you see this in the BQ browser tool or only on your spreadsheet?
BQ float is of size of 8 bytes, so it can hold numbers >9,000,000,000,000...
I find it that sometimes when Excel opens a flat file (csv) it converts it to the format you mentioned. To verify this is the case, try to open your csv with notepad (or other flat file editor), before you try with excel.
If this is indeed the issue, you can configure the excel connector to treat this field as string instead of number. other option would be to convert it to string and concat "" to the number. this way the spreadsheet will automatically treat it as string. afterwards you can convert it back to number in the spreadsheet.
Thanks

what data-type to use to store MAC addresses in an SQL Server database table?

I want to store MAC addresses in one of my database tables, what data-type should I use? Reading articles on google, I have seen Binary(8) mentioned a few times. Is this the correct way?
Also, this does not make sense to me, as MAC addresses are six groups of two hexadecimal digits, wouldn't you use Binary(6)?
I wouldn't use Binary at all.
I would use CHAR(12).
Though this really depends on what you use the data for - if this is for display only, you can simply use the textual representation.
For easier performaing binary operations you can store them into Binary(6)
You can use the following built in function to view the Hex readable value of the binary data:
select top 10 master.sys.fn_varbintohexstr(mac) from macaddresses
and to convert the hexadecimal text into binary:
select CONVERT(binary(6), 'AABBCCDDEEFF', 2);
MAC address is a sequence of 6 hexadecimal numbers. That's why it would be efficient to store it as a number. Use BIGINT.