I can suggest a two-step process, which will randomize any number of points inside each census tract:
First, perform a Points in Polygon Analysis. You will find this tool in Vector > Analysis Tools > Points in Polygon
. This will add a new field in your census tracts layer with the number of points inside each tract.
Create random points inside your tracts, with Vector > Research Tools > Random Points
. Choose the Use value from input field
option (with of course the previously created field).
Edit 2: new workaround. As Steven Kay suggested in the comments, there is a simpler solution if you want to keep the original fields (no need of CSV exporting/importing). I've kept my initial workaround at the end of the answer, if for some reason the following process doesn't work. Steps 1 and 2 are still required beforehand.
[This assumes that each census tract has some unique identifier. If not, create one beforehand.] Perform a spatial joint between points1 and your census tracts layer: Vector > Data Management Tools > Join Attributes by Location
. Target layer is points1, join layer is the census tracts. Don't change the default options. This will add to each point the attributes of the corresponding census tract, including its ID. Follow the same process with points2.
Install the Sort and Number plugin. You will find it in the regular QGIS plugin repository. Open it, choose points1 as input layer, then "census tract ID" as 1st field. Click OK to create a new field (which creates new point IDs, following the order of their census tract). Follow the same process with points2.
Open points2 properties window. In the "Joins" section, click on the bottom "+". Choose points1 as join layer, "order" (or whatever field name you chose in step 4) as join field and target field. Click on "Choose which fields are joined" to select relevant points1 attributes.
VoilĂ , you can now access points1 attributes in points2 layer. Fully tested with QGIS 2.8.
Edit 1: original workaround. Keeping the original fields is much more difficult. There is no easy way to do this, but I thought of a convoluted workaround (steps 1 and 2 are still required):
Add x and y fields to the new random points layer (let's call it points2, and the original layer points1). To do so, use the field calculator with the $x
and $y
formulas.
[This assumes that each census tract has some unique identifier. If not, create one beforehand.] Perform a spatial joint between points1 and your census tracts layer: Vector > Data Management Tools > Join Attributes by Location
. Target layer is points1, join layer is the census tracts. Don't change the default options. This will add to each point the attributes of the corresponding census tract, including its ID. Follow the same process with points2.
Export points1 and points2 as CSV (Save As...
, in Format choose CSV). Open the CSVs with Excel or something similar. Sort the points by census tract ID (this is important!). Copy the x and y columns from points2 to points1, then save points1.
Import the new points1 CSV to QGIS, following this tutorial. In "Geometry definition", choose "Points coordinates" and the corresponding x and y fields. This should finally result in displaying your original points with random locations.
I didn't completely test this part, so I hope it will work for you. I'd be interested to know if someone can come with a simpler way to solve your issue!